How to delete duplicate rows from a MySQL table

前端 未结 8 1750
悲哀的现实
悲哀的现实 2020-11-30 12:07

I have a MySQL table like:

ID, Col1, Col2, Col3, Col4, etc...

ID is a primary key and has been w

8条回答
  •  情歌与酒
    2020-11-30 12:25

    Without nested selects or temporary tables.

    DELETE  t1
    FROM    table_name t1, table_name t2
    WHERE   
                (t1.Col1 = t2.Col1 OR t1.Col1 IS NULL AND t2.Col1 IS NULL)
            AND (t1.Col2 = t2.Col2 OR t1.Col2 IS NULL AND t2.Col2 IS NULL)
            AND (t1.Col3 = t2.Col3 OR t1.Col3 IS NULL AND t2.Col3 IS NULL)
            AND (t1.Col4 = t2.Col4 OR t1.Col4 IS NULL AND t2.Col4 IS NULL)
            ...
            AND t1.ID < t2.ID;
    

提交回复
热议问题