0

My Table

 id   userid    emailid
 ---+---------+------------
 1  |  1      |  a@a.com
 2  |  1      |  b@b.com
 3  |  123    |  a@a.com
 4  |  123    |  b@b.com
 5  |  123    |  c@c.com
 6  |  123    |  c@c.com

Result Desired

id    userid    emailid
 ---+---------+------------
 1  |  1      |  a@a.com
 2  |  1      |  b@b.com
 3  |  123    |  a@a.com
 4  |  123    |  b@b.com
 5  |  123    |  c@c.com

But I am getting

id    userid    emailid
 ---+---------+------------
 1  |  1      |  a@a.com
 2  |  1      |  b@b.com
 6  |  123    |  c@c.com

I tried (I am new to SQL)

DELETE FROM table 
WHERE id NOT IN (SELECT *
FROM (SELECT MIN(n.id) 
FROM table n GROUP BY n.emailid) x)
Ullas
  • 11,450
  • 4
  • 33
  • 50
Eve
  • 13
  • 4
  • possible duplicate of [Delete all Duplicate Rows except for One in MySQL?](http://stackoverflow.com/questions/4685173/delete-all-duplicate-rows-except-for-one-in-mysql) – M Khalid Junaid Aug 31 '14 at 10:50
  • this is not a duplicate I struggled for 3 hours to writing test queries and searching stackoverflow. The link you refer to has the wrong answer I found that unless I also included AND n1.id <> n2.id, it deleted every row in the table. I dont understand how it got 218 votes! Most other answers required me to change the way the table is defined or did not show how to delete. I closest answer that came to my question is what I have pasted above under "I tried..." until Ullas responded below. – Eve Aug 31 '14 at 15:01

1 Answers1

1

Try This.

QUERY

DELETE FROM table_name 
WHERE id NOT IN
(
   SELECT MIN(id)
   FROM table_name
   GROUP BY userid,emailid
);
Ullas
  • 11,450
  • 4
  • 33
  • 50