my sql 删除表中重复的数据

1、根据指定条件查询表中的重复记录

select * from t_official_sys_user  
where USER_NAME in (select  USER_NAME  from (select  USER_NAME  from  t_official_sys_user  group  by  USER_NAME  having  count(USER_NAME) > 1) as temtalble );  
sql分析:

查询用户表: t_official_sys_user ,统计 USER_NAME这个字段判断大于一条记录以上的记录进行分组,然后放到一张临时表 tamtalbe . 查询USER_NAME在临时表中的记录为重复记录.

2、删除重复记录


delete from t_official_sys_user  
where USER_NAME IN(select USER_NAME from ( SELECT USER_NAME FROM t_official_sys_user group by `USER_NAME` having  count(1) > 1) as temtable );  

你可能感兴趣的:(sql,mysql)