I have the table contacts
which contains duplicate records:
id name is_contacted created_at
I need to delete duplicates, but keep the first record(among the duplicates for each name) where is_contacted=1.
If among the record duplicates there are no records where is_contacted=1, just keep the first one.
This is what I have so far:
DELETE c1 FROM contacts c1
INNER JOIN contacts c2
WHERE
c1.id > c2.id AND
c1.name = c2.name;
2
Answers
Below query will filter only records what you want.
You didn’t mention what is primary key in your table, so I don’t know how to join this back 1:1 with your whole table.
But if you are not able to determine primary key, they you can create new table using this query, drop original one and rename it to original one.
Assuming that
is_contacted
‘s data type isBOOLEAN
andid
is the primary key of the table and this is the column that defines the order and which row should be considered first, useROW_NUMBER
window function to rank the rows of eachname
:ORDER BY is_contacted DESC, id
returns the rows withis_contacted = 1
at the top (if they exist).For versions of MySql prior to 8.0, without support of CTEs and winow functions, use a join of the table to a query that uses aggregation to get the
id
of the row that you want to keep: