Could you clarify what you need to do in the long run? The best solution may depend on this (for example, just want to delete all rows with duplicate keys?)
One way is to process this table (not sure if mySQL supports it, it is from SYBASE) if all you need is a row with a unique key:
SELECT MIN(id), A, B FROM FOO GROUP BY A, B HAVING COUNT(*)>1
Your exact question (although I do not understand a bit why you need all the lines except id = 2):
SELECT F1.* FROM FOO F1 , (SELECT A, B FROM FOO GROUP BY A, B HAVING COUNT(*)>1) F2 WHERE F1.A=F2.A and F1.B=F2.B
To remove all duplicates, you can, for example, do
DELETE FOO WHERE NOT EXISTS (SELECT 1 from (SELECT MIN(id) 'min_id' FROM FOO GROUP BY A, B HAVING COUNT(*)>1) UINIQUE_IDS WHERE id = min_id)
Alternatively you can do
SELECT MIN(id) 'id', A, B INTO TEMPDB..NEW_TABLE FROM FOO GROUP BY A, B HAVING COUNT(*)>1 TRUNCATE TABLE FOO // Drop indices on FOO INSERT FOO SELECT * FROM NEW_TABLE // Recreate indices on FOO
source share