使用下面的查询
-- Check : This query will give you unique records
SELECT MAX(ID)
FROM MyTable
GROUP BY Column1, Column2, Column3 ......
-- Now delete the record apart from unique record
DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY Column1, Column2, Column3 ......)
注意:这里的 ID 是主键。如果您的表中不存在 ID,则首先使用 AUTO_INCREMENT 值创建 ID,然后运行上述查询。
例子:
我的表
col1 col2 col3 col4 col5 col6 col7
john 1 1 1 1 1 1
john 1 1 1 1 1 1
john 1 1 1 1 1 1
sally 2 2 2 2 2 2
sally 2 2 2 2 2 2
如果 ID 不存在,则添加具有自动递增值的 ID。添加ID后
ID col1 col2 col3 col4 col5 col6 col7
1 john 1 1 1 1 1 1
2 john 1 1 1 1 1 1
3 john 1 1 1 1 1 1
4 sally 2 2 2 2 2 2
5 sally 2 2 2 2 2 2
-- 检查:此查询将为您提供唯一记录
SELECT MAX(ID) FROM MyTable
GROUP BY col1, col2, col3, col4, col5, col6, col7
查询输出
最大(ID)
3
5
-- 现在删除唯一记录之外的记录
DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID) FROM MyTable
GROUP BY col1, col2, col3, col4, col5, col6, col7)