You are looking for sd1.names where exists another record with certain criteria. Then you delete all records with the names found.
Isn't this simply: delete records for which exists another record with certain criteria?
DELETE FROM my_table
WHERE EXISTS
(
SELECT *
FROM
(
SELECT *
FROM my_table sd2
WHERE sd2.name > my_table.name
AND sd2.name & my_table.name = my_table.name
AND my_table.number >= sd2.number
) dummy
);
(UPDATED: MySQL cannot access the same table in UPDATE
or DELETE
and one must wrap the subquery so the table gets kind of hidden hidden one level deeper. Looks absolutely senseless, but is supposed to solve this issue. Hope it does.)
That's less complicated and therefore maybe faster. Especially since EXISTS
has to find only one match per main record and can stop there, whereas a join looks for all matches.