刪除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;ci

刪除重複數據時,可能會出現You can't specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中),組要在select重查一遍以免該錯誤。get

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;table

相關文章
相關標籤/搜索