How to delete records in the table which are repeated?

前端 未结 6 1782
长情又很酷
长情又很酷 2021-01-25 03:35

Hi Here i came across a situation in which by mistakenly Without dropping the table i have run the batch file of the table which consists of some insert statements in detail

6条回答
  •  臣服心动
    2021-01-25 04:15

    Following query will give you all records that you want to keep:

    SELECT min(id)
    FROM alert_priority
    GROUP BY priority_name
    HAVING count(*) > 1
        OR min(id) = max(id)
    

    To remove all duplicates, run this query:

    DELETE FROM alert_priority
    WHERE id NOT IN (
        SELECT min(id)
        FROM alert_priority
        GROUP BY priority_name
        HAVING count(*) > 1
            OR min(id) = max(id)
    )
    

提交回复
热议问题