mysql 重复记录查询与删除

来源:互联网 发布:如何改变网络的拥堵 编辑:程序博客网 时间:2024/06/13 07:44

一个字段重复记录

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

)

阅读全文
0 0
原创粉丝点击