How to delete duplicates on a MySQL table?

后端 未结 25 2767
遇见更好的自我
遇见更好的自我 2020-11-22 01:35

I need to DELETE duplicated rows for specified sid on a MySQL table.

How can I do this with an SQL query?

         


        
25条回答
  •  旧时难觅i
    2020-11-22 02:06

    Love @eric's answer but it doesn't seem to work if you have a really big table (I'm getting The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay when I try to run it). So I limited the join query to only consider the duplicate rows and I ended up with:

    DELETE a FROM penguins a
        LEFT JOIN (SELECT COUNT(baz) AS num, MIN(baz) AS keepBaz, foo
            FROM penguins
            GROUP BY deviceId HAVING num > 1) b
            ON a.baz != b.keepBaz
            AND a.foo = b.foo
        WHERE b.foo IS NOT NULL
    

    The WHERE clause in this case allows MySQL to ignore any row that doesn't have a duplicate and will also ignore if this is the first instance of the duplicate so only subsequent duplicates will be ignored. Change MIN(baz) to MAX(baz) to keep the last instance instead of the first.

提交回复
热议问题