创建表
CREATE TABLE tmp
(id INT PRIMARY KEY,
A VARCHAR(5),
B VARCHAR(5),
C VARCHAR(5),
D VARCHAR(5)
);
插入测试数据
INSERT INTO tmp
SELECT 1 ,‘A1’,‘B1’,‘C1’,‘D1’
UNION ALL
SELECT 2 ,‘A1’,‘B1’,‘C2’,‘D2’
UNION ALL
SELECT 3,‘A2’,‘B2’,‘C3’,‘D3’
UNION ALL
SELECT 4 ,‘A2’,‘B2’,‘C4’,‘D4’
UNION ALL
SELECT 5 ,‘A3’,‘B3’,‘C5’,‘D5’
UNION ALL
SELECT 6 ,‘A3’,‘B3’,‘C6’,‘D6’
UNION ALL
SELECT 7 ,‘A3’,‘B3’,‘C7’,‘D7’
清除重复的记录
以下的思想没有问题,但是直接执行会报错:
DELETE FROM tmp WHERE id NOT IN (SELECT MAX(id) AS F FROM tmp GROUP BY A,B)
报错:大概意思是不能先select出同一表中的某些值,再update这个表。
You can’t specify target table ‘tmp’ for update in FROM clause
在子查询外再包一层查询就可以了:
DELETE FROM tmp WHERE id NOT IN (SELECT f FROM (SELECT MAX(id) AS F FROM tmp GROUP BY A,B) t)
结果: