How do I delete duplicate rows and keep the first row?

前端 未结 4 1726
暖寄归人
暖寄归人 2020-12-09 06:11

I made a mistake and I have unwanted duplicates.

I have a table with 4 key fields. A1, k1, k2, k3.

相关标签:
4条回答
  • 2020-12-09 06:28

    You can create a new table with the same structure but empty, then create the unique key on it, then do a INSERT IGNORE / SELECT * FROM the original table into the new table, then delete the original table.

    INSERT IGNORE will automatically ignore any primary or unique key issues and just skip the duplicates.

    0 讨论(0)
  • 2020-12-09 06:34

    You need a separator in your concat function, because otherwise "a", "b", and "cd" is the same as "abcd", "", "".

    0 讨论(0)
  • 2020-12-09 06:37

    Backup your data, then...

    MySQL supports JOINs in DELETE statements. If you want to keep the first of the duplicates:

    DELETE a
      FROM MYVIEWS a
      JOIN (SELECT MIN(t.a1) AS min_a1, t.k1, t.k2, t.k3
              FROM MYVIEWS t
          GROUP BY t.k1, t.k2, t.k3
            HAVING COUNT(*) > 1) b ON b.k1 = a.k1
                                  AND b.k2 = a.k2
                                  AND b.k3 = a.k3
                                  AND b.min_a1 != a.a1
    

    If you want to keep the last of the duplicates:

    DELETE a
      FROM MYVIEWS a
      JOIN (SELECT MAX(t.a1) AS max_a1, t.k1, t.k2, t.k3
              FROM MYVIEWS t
          GROUP BY t.k1, t.k2, t.k3
            HAVING COUNT(*) > 1) b ON b.k1 = a.k1
                                  AND b.k2 = a.k2
                                  AND b.k3 = a.k3
                                  AND b.max_a1 != a.a1
    
    0 讨论(0)
  • 2020-12-09 06:44

    Someting like this?

    DELETE FROM myviews WHERE EXISTS(SELECT CONCAT(k1, k2, k) AS dup_value
    FROM myviews
    GROUP BY dup_value
    HAVING (COUNT(dup_value) > 1));
    
    0 讨论(0)
提交回复
热议问题