I have the below table and now I need to delete the rows which are having duplicate \"refIDs\" but have atleast one row with that ref, i.e i need to remove row 4 and 5. plea
In MySQL, you can do this with a join
in delete
:
delete t
from table t left join
(select min(id) as id
from table t
group by refId
) tokeep
on t.id = tokeep.id
where tokeep.id is null;
For each RefId
, the subquery calculates the minimum of the id
column (presumed to be unique over the whole table). It uses a left join
for the match, so anything that doesn't match has a NULL
value for tokeep.id
. These are the ones that are deleted.