CREATE TABLE `test` ( `id` INT(20) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20) NULL DEFAULT NULL, `age` INT(5) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB
查詢出所有重複的記錄 ,刪除所有 select a.* 換成delete
select a.* from test a join (select name,count(*) from test group by name having count(*)> 1 ) b on a.name = b.name;
查詢不重複的記錄,和重複記錄裡最後插入的記錄
select * from test a where a.id = (select max(id) from test b where a.name = b.name);
查詢不重複的記錄。和重複記錄裡,最先插入的記錄
select * from test a where a.id = (select min(id) from test b where a.name = b.name);
查詢出重複記錄裡,第一條重複記錄以外的所有重複記錄 刪除的話select * 換成delete
select * from test a where a.id != (select min(id) from test b where a.name = b.name);
查詢出重複記錄裡,最後一條重複記錄以外所有重複的記錄 ,刪除的話select * 換成delete
select * from test a where a.id != (select max(id) from test b where a.name = b.name);