日期:2014-05-16  浏览次数:20673 次

mysql临时表技巧

给定一个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;
?

?