ITeye博客

mysql临时表技巧

javafan_303 · 2011-01-25 11:23 · mysql · 数据库 · MySQL · Markdown

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

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;

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。