删除Mysql重复数据


delete from t_user where userId in(
select userId from t_user GROUP BY userId having count(*)>1)
and id not in(select min(id)as id from t_user group by userId having count(*)>1) and status=0;

删除重复数据时,可能会出现You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中),组要在select重查一遍以避免该错误。

delete from t_user where userId in(
select t.userId from 
(select userId from t_user GROUP BY userId having count(*)>1)t)
and id not in(select tb.id from 
(select min(id)as id from t_user group by userId having count(*)>1)tb) and status=0;

猜你喜欢

转载自my.oschina.net/u/2610894/blog/1600555