小陆同学

python 中文名:蟒蛇,设计者:Guido van Rossum

导航

mysql--记2次脏数据清理

有时候程序执行的过程中,可能有逻辑漏洞,导致同样一条数据多次insert到表里,造成了垃圾数据,在清理的时候又不能一杆子都打死,需要将重复数据中,id最小的保留

DELETE FROM public_sentiment WHERE id IN (
    SELECT * FROM (
        SELECT id FROM public_sentiment WHERE (sentiment_id,vuln_name)         IN (
            SELECT sentiment_id,vuln_name FROM public_sentiment GROUP BY sentiment_id,vuln_name HAVING COUNT(1) > 1
        ) AND id NOT IN (
            SELECT MIN(id) FROM public_sentiment GROUP BY sentiment_id,vuln_name HAVING COUNT(1) > 1
        )
    ) AS repeat_copy

);

  

DELETE FROM 表名 WHERE id IN (
    SELECT * FROM (
        SELECT id FROM 表名 WHERE (字段名1)  IN (
            SELECT 字段名1 FROM 表名 GROUP BY 字段名1 HAVING COUNT(1) > 1
        ) AND id NOT IN (
            SELECT MIN(id) FROM 表名 GROUP BY 字段名1  HAVING COUNT(1) > 1
        )
    ) AS repeat_copy

);

 

posted on 2022-05-23 14:21  小陆同学  阅读(76)  评论(0编辑  收藏  举报