mysql删除数据表中重复记录保留一条

删除数据库中重复的记录由两种形式:
第一种是数据表中所有的字段都重复,第二种是数据库中部分字段重复
这里针对第二种情况重复:

delete from app_user_verify where id not in (select a.id from (select MAX(id) as id from app_user_verify  GROUP by code_type,telephone,status) a)

如果出现You can’t specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)
在加上一层select 即可

delete from app_user_verify where id not in (select b.id from (select a.id from (select MAX(id) as id from app_user_verify GROUP BY code_type,telephone,status) a) b) 

其中code_type,telephone,status是重复的字段
这种一条sql语句形式如果数据量很大删除会很慢,可以分别执行sql语句,然后用php将他们整合即可
参考文章

猜你喜欢

转载自blog.csdn.net/weixin_38112233/article/details/80252742