Let there be a users table with the user_id and user_name fields , and it contains entries with the same user_name values. The request to delete "extra" lines will look like, say:
DELETE FROM users WHERE user_id IN (SELECT user_id FROM users WHERE user_name IN (SELECT user_name FROM (SELECT user_id, COUNT(*) AS cnt, user_name FROM users GROUP BY user_name HAVING cnt>1) AS t1 ) AND user_id NOT IN (SELECT user_id FROM users GROUP BY user_name) )
Or so:
DELETE FROM users WHERE user_id IN (SELECT t1.user_id FROM (SELECT user_id, user_name FROM users) AS t1 INNER JOIN (SELECT user_name, COUNT(*) AS cnt, MIN(user_id) AS min_id FROM users GROUP BY user_name HAVING cnt > 1) AS t2 ON t1.user_name=t2.user_name AND t1.user_id!=t2.min_id )
In general, whatever one may say, it turns out, in fact, two nested selects, and the operation time, in theory, respectively, is twice as long, although if it were possible to refer to the result t1 after AND in the first example, then it would be more Feng Shui, probably (and some crutches). Is there any way to fix this?
And second, for some reason, SQLyog swears at the above examples:
Error Code: 1093 You can't specify target table 'users' for update in FROM clause
I’m lame in English, but as far as I understand, it claims that I can’t send the result of a request to the deletion command !? O_o
PS Strongly do not scold, sql did not remember since school years ...