Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete duplicate records based on two columns in MySql [duplicate]

I want to delete the extra duplicate record

i.e. in the image shown there are two records with corporate_id = 5 and category_id = 19, how to delete any one row which is duplicate (here corporate_sed_id is the primary key)

enter image description here

like image 437
K Arun Singh Avatar asked Oct 03 '17 10:10

K Arun Singh


1 Answers

Use this :-

DELETE
FROM
    corporate
WHERE
    corporate_sed_id IN(
    SELECT
        *
    FROM
        (
        SELECT
            MIN(corporate_sed_id)
        FROM
            corporate
        GROUP BY
            corporate_id,
            category_id
        HAVING
            COUNT(corporate_sed_id) > 1
    ) temp
)
like image 199
nikita Avatar answered Sep 20 '22 18:09

nikita