您的位置:首页 > 数据库 > MySQL

mysql 重复记录查询与删除

2017-11-29 10:42 211 查看



一个字段重复记录

SELECT table1.* FROM table table1,(

    SELECT  code FROM table GROUP BY code HAVING count(code)
> 1


) table2 where table1.code=table2.code

delete from table where id not in (

    select maxid from (

        select max(id) as maxid from table group by code

    ) temp




 两个字段重复记录

SELECT table1.* FROM table table1,(

    SELECT code1,code2 FROM
table GROUP BY code1,code2 HAVING count(code1) > 1


) table2 where table1.code1=table2.code1 and table1.code2=table2.code2

DELETE from table WHERE id not in 

(
    SELECT maxid FROM(


        SELECT MAX(id) maxid, CONCAT(code1,code2)
concat FROM table GROUP BY concat


    ) temp

)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: