MySQL

给定一个use表,包含id和uid两列,其中uid列可能有重复,要求找出重复的记录,并删掉多余的记录,使得对于uid重复的记录只保留id最小的记录。

因为你要删除数据,下面是采用临时表的方式,测试通过

[code="sql"]
drop table if EXISTS tmp;
create table tmp as select min(id) as id,uid from user GROUP by uid HAVING count(uid)>1;
delete from user where id not in (select id from tmp) and uid in(select uid from tmp);
drop table tmp;
commit;
[/code]

你的use表我这里换成了user表,因为use是mysql的关键字,不能使用。我这里的临时表是tmp,你可以更换,只要别和你现有的表重名就行,要不你的表的数据就没了!

sql语句?

[code="sql"]

delete from test where uid in (select uid from test group by uid having count(uid)>1) and id not in (select min(id) from test group by uid having count(uid )>1)

[/code]