mysql 使用 where 子句删除重复项
mysql removing duplicates with where clause
我正在尝试删除具有相同 hid
值的重复记录。
这是Fiddle:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=86e8ed00cf0a496da490eae5d7aae093
Table product_match_unmatches:
ID hid flag
1 1 1
2 1 1
3 2 1
4 2 1
5 1 2
6 2 2
7 2 2
8 1 1
9 1 1
10 2 1
现在我想从 table 中删除重复项 hid
但仅针对 flag = 1。
此查询将删除所有 duplicates
除了最近的一个,但不考虑标志值:
DELETE pmu1
FROM dmf_product_match_unmatches as pmu1
LEFT JOIN ( SELECT MAX(ID) as ID, hid
FROM dmf_product_match_unmatches as pmu2
GROUP BY hid) pmu3 USING (ID, hid)
WHERE pmu3.ID IS NULL;
我试图在上面的查询中添加 where 子句 flag = 1
,但这没有产生预期的结果。
DELETE pmu1
FROM dmf_product_match_unmatches as pmu1
LEFT JOIN ( SELECT MAX(ID) as ID, hid
FROM dmf_product_match_unmatches as pmu2
where flag = 1
GROUP BY hid
) pmu3 USING (ID, hid)
WHERE pmu3.ID IS NOT NULL;
要求的输出是:
ID hid flag
5 1 2
6 2 2
7 2 2
9 1 1
10 2 1
你需要
DELETE t1
FROM dmf_product_match_unmatches t1
JOIN dmf_product_match_unmatches t2 USING (hid, flag)
WHERE flag = 1
AND t1.id < t2.id;
?
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a5e9e95335573ebedd45cdcd577b5602
使用row_number
DELETE pmu1
FROM dmf_product_match_unmatches pmu1
JOIN (select id,
row_number() over(partition by hid order by id desc) rn
from dmf_product_match_unmatches
where flag = 1
) as pmu3 ON pmu1.ID = pmu3.ID
WHERE pmu3.rn > 1;
在 sql 服务器中,您可以使用 EXCEPT
设置运算符:
declare @flag int = 1
delete dmf_product_match_unmatches
where id in (
select id
from dmf_product_match_unmatches
where flag = @flag
except
select max(id) id
from dmf_product_match_unmatches
where flag = @flag
group by hid, flag
)
在mysql中你可以使用NOT EXISTS
declare @flag int = 1
delete d1
from dmf_product_match_unmatches d1
where flag = @flag
and not exists (
select max(id) id
from dmf_product_match_unmatches d2
group by hid, flag
having d1.id = max(d2.id)
)
我正在尝试删除具有相同 hid
值的重复记录。
这是Fiddle:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=86e8ed00cf0a496da490eae5d7aae093
Table product_match_unmatches:
ID hid flag
1 1 1
2 1 1
3 2 1
4 2 1
5 1 2
6 2 2
7 2 2
8 1 1
9 1 1
10 2 1
现在我想从 table 中删除重复项 hid
但仅针对 flag = 1。
此查询将删除所有 duplicates
除了最近的一个,但不考虑标志值:
DELETE pmu1
FROM dmf_product_match_unmatches as pmu1
LEFT JOIN ( SELECT MAX(ID) as ID, hid
FROM dmf_product_match_unmatches as pmu2
GROUP BY hid) pmu3 USING (ID, hid)
WHERE pmu3.ID IS NULL;
我试图在上面的查询中添加 where 子句 flag = 1
,但这没有产生预期的结果。
DELETE pmu1
FROM dmf_product_match_unmatches as pmu1
LEFT JOIN ( SELECT MAX(ID) as ID, hid
FROM dmf_product_match_unmatches as pmu2
where flag = 1
GROUP BY hid
) pmu3 USING (ID, hid)
WHERE pmu3.ID IS NOT NULL;
要求的输出是:
ID hid flag
5 1 2
6 2 2
7 2 2
9 1 1
10 2 1
你需要
DELETE t1
FROM dmf_product_match_unmatches t1
JOIN dmf_product_match_unmatches t2 USING (hid, flag)
WHERE flag = 1
AND t1.id < t2.id;
?
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a5e9e95335573ebedd45cdcd577b5602
使用row_number
DELETE pmu1
FROM dmf_product_match_unmatches pmu1
JOIN (select id,
row_number() over(partition by hid order by id desc) rn
from dmf_product_match_unmatches
where flag = 1
) as pmu3 ON pmu1.ID = pmu3.ID
WHERE pmu3.rn > 1;
在 sql 服务器中,您可以使用 EXCEPT
设置运算符:
declare @flag int = 1
delete dmf_product_match_unmatches
where id in (
select id
from dmf_product_match_unmatches
where flag = @flag
except
select max(id) id
from dmf_product_match_unmatches
where flag = @flag
group by hid, flag
)
在mysql中你可以使用NOT EXISTS
declare @flag int = 1
delete d1
from dmf_product_match_unmatches d1
where flag = @flag
and not exists (
select max(id) id
from dmf_product_match_unmatches d2
group by hid, flag
having d1.id = max(d2.id)
)