Lets imagine two tables:
username
id, name
1 username1
2 username1
3 username2
emails
id, user, name
1 1 assdf@sdf.hu
2 2 afgdf@sdf.hu
3 3 gfg@sdf.hu
the problem is there are duplicated entries. I cant simply drop them, because records are connected to them, so they get lost. (for example afgdf@sdf.hu email address). How to solve it?
SQLFiddle demo
First you should update table
emailsto replaceuserwith right values to eliminate values to be deleted.Now delete from
usernametable duplicate records. We keep only records with MINIMALidfor eachname: