mysql 重复记录查询与删除

一个字段重复记录

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

)

你可能感兴趣的:(mysql)