从表中删除重复记录,考虑如下所示的表:
create table dupes (id integer,name varchar(10));
insert into dupes values (1,'NOPOLEON');
insert into dupes values (2,'DYNAMITE');
insert into dupes values (3,'DYNAMITE');
insert into dupes values (4,'SHE SELLS');
insert into dupes values (5,'SEA SHELLS');
insert into dupes values (6,'SEA SHELLS');
insert into dupes values (7,'SEA SHELLS');
select * from dupes order by 1;
+------+------------+
| id | name |
+------+------------+
| 1 | NOPOLEON |
| 2 | DYNAMITE |
| 3 | DYNAMITE |
| 4 | SHE SELLS |
| 5 | SEA SHELLS |
| 6 | SEA SHELLS |
| 7 | SEA SHELLS |
+------+------------+
解决方案:
delete from dupes
where id not in
(select minid from
(select min(id) as minid from dupes group by name)
b);
说明: group by后面的name是要删除的重复的那一列。