This question already has an answer here:
- How to delete Duplicates in MySQL table 4 answers
I want to delete multiple duplicate keys from the below table:
id | name | uid
1 | ekta | 5
2 | ekta | 5
3 | sharma | 10
4 | sharma | 10
want it to be like
id | name | uid
1 | ekta | 5
3 | sharma | 10
I am using mysql. Is it ossible.?
I can't use unique constraint
query to make unique enteries because i want this duplicate entries ones entered to the table.
One way of doing this is by joining the table on a subquery using
LEFT JOIN
. The subquery gets the lowestID
for everyUID
. When a record doesn't have match on the subquery, it just means that it has no matching record and can be safely deleted.However, if the records of
UID
can have different name, then you need to includename
on thegroup by
clause or else only uniqueuid
with the lowestID
will remain.