how to delete duplicate values in mysql table

前端 未结 2 1964
遇见更好的自我
遇见更好的自我 2020-12-22 05:42

I have column in mysql table (my_sale_time) of type timestamp....infact the rows value look like this

2010-12-01 14:38:07
2010-12-         


        
相关标签:
2条回答
  • 2020-12-22 06:14

    If you have an auto_increment field, use this:

    DELETE FROM
        `mytable`
    WHERE
        `my_auto_increment_field` NOT IN (
            SELECT
                MAX(`my_auto_increment_field`)
            GROUP BY
                `my_sale_time`
        );
    
    0 讨论(0)
  • 2020-12-22 06:20

    The basic principle of deleting duplicate rows:

    CREATE TEMPORARY TABLE tmptbl AS SELECT DISTINCT * FROM my_sale_time;
    DELETE FROM my_sale_time;
    INSERT INTO my_sale_time SELECT * FROM tmptbl;
    

    You may have to specify columns and WHERE clauses (I didn't really understand your criteria). And of course you should test-run it on a development server and don't forget to run it as a single transaction with locked tables.

    0 讨论(0)
提交回复
热议问题