我有像
这样的表格table_id item_id vendor_id category_id
1 1 33 4
2 1 33 4
3 1 33 2
4 2 33 4
5 2 33 2
6 3 33 4
7 3 33 4
8 1 34 4
9 1 34 4
10 3 35 4
此处table_id
是主键和表,共有98000个条目,包括通过执行查询找到的61个重复条目
SELECT * FROM my_table
WHERE vendor_id = 33
AND category_id = 4
GROUP BY item_id having count(item_id)>1
在上表table_id
中1,2和6,7重复。我需要从我的表中删除2和7(共61个重复条目)。如何使用where子句vendor_id = 33 AND category_id = 4的查询从表中删除重复的条目?我不想删除其他重复条目,例如table_id
8,9
我无法索引表格,因为我需要保留一些所需的重复条目。我需要删除具有特定条件的副本
答案 0 :(得分:1)
尝试使用LEFT JOIN
,如下所示:
DELETE my_table
FROM my_table
LEFT JOIN
(SELECT MIN(table_id) AS IDs FROM my_table
GROUP BY `item_id`, `vendor_id`, `category_id`
)A
ON my_table.table_id = A.IDs
WHERE A.ids IS NULL;
删除后的结果:
| TABLE_ID | ITEM_ID | VENDOR_ID | CATEGORY_ID |
------------------------------------------------
| 1 | 1 | 33 | 4 |
| 3 | 1 | 33 | 2 |
| 4 | 2 | 33 | 4 |
| 5 | 2 | 33 | 2 |
| 6 | 3 | 33 | 4 |
如果您想添加更多条件,可以将其添加到外WHERE
条件中,如下所示:
DELETE my_table
FROM my_table
LEFT JOIN
(SELECT MIN(table_id) AS IDs FROM my_table
GROUP BY `item_id`, `vendor_id`, `category_id`
)A
ON my_table.table_id = A.IDs
WHERE A.ids IS NULL
AND vendor_id = 33 --< Additional conditions here
AND category_id = 4 --< Additional conditions here
答案 1 :(得分:0)
这个怎么样:
DELETE FROM my_table
WHERE table_id NOT IN
(SELECT MIN(table_id)
FROM my_table
GROUP BY item_id, vendor_id, category_id)
答案 2 :(得分:0)
try below code...
DELETE FROM myTable
WHERE table_ID NOT IN (SELECT MAX (table_ID)
FROM myTable
GROUP BY table_ID
HAVING COUNT (*) > 1)
答案 3 :(得分:0)
尝试
DELETE m
FROM my_table m JOIN
(
SELECT MAX(table_id) table_id
FROM my_table
WHERE vendor_id = 33
AND category_id = 4
GROUP BY item_id, vendor_id, category_id
HAVING COUNT(*) > 1
) q ON m.table_id = q.table_id
删除后你会有
| TABLE_ID | ITEM_ID | VENDOR_ID | CATEGORY_ID | ------------------------------------------------ | 1 | 1 | 33 | 4 | | 3 | 1 | 33 | 2 | | 4 | 2 | 33 | 4 | | 5 | 2 | 33 | 2 | | 6 | 3 | 33 | 4 | | 8 | 1 | 34 | 4 | | 9 | 1 | 34 | 4 | | 10 | 3 | 35 | 4 |
这是 SQLFiddle 演示
答案 4 :(得分:-1)
从你的问题中,我想你需要删除与item_id,vendor_id和category_id具有相同值的重复行,就像具有tabled_id 1和2的行一样。所以可以通过使所提到的三列一起唯一来完成。请尝试以下方法,
alter ignore table table_name add unique index(item_id, vendor_id, category_id);
注意:我还没有测试过,会在某个时候给出sqlfiddle