题:有一张test表,里面含有三个字段id,name,sex。
问:如何查询重复数据?
select name,sex,count(*) from test group by name,sex HAVING count(*)>1。
问:如何删除重复数据并保留ID最大的数据?
delete a from test a join
(select name,sex,count(*) ,max(id) as id
from test group by name,sex
HAVING count(*)>1) b on a.name=b.name where a.id<b.id
时间: 2024-11-10 01:06:33