0

I have table with 3 columns :

ID, name, role 

Some names are duplicated but they all have an unique ID, how do I delete all rows with duplicated name(not all,leave one for each) in my table?

1 Answer 1

2

Group by the name and select the lowest unique id. Delete all records that are not in that list

delete from your_table where id not in ( select min(id) from your_table group by name ) 

And if you use MySQL you need another subquery since MySQL does not allow you to delete from the same table you are selecting from:

delete from your_table where id not in ( select * from ( select min(id) from your_table group by name ) tmp_tbl ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I forgot to mention, the ID is uniqueidentifier, I'm not sure if it can select the lowest.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.