sw9 Posted May 26, 2010 Share Posted May 26, 2010 I have a table with many columns, including: group_id, contact_id It has 20,000 rows in it. I cannot have entries where group_id and contact_id are the same more than once, so for example: group_id | contact_id 100 606 100 606 Are bad. But group_id | contact_id 100 606 68 606 are fine. I need to find instances where they are duplicate entries. If I run this query: SELECT DISTINCT group_id, contact_id FROM table1 GROUP BY group_id, contact_id then I believe it's showing me how many are distinct, and that leaves me to believe that there are about 400 cases of duplicates. I need to see all the ones that are duplicates. How do I write this query? Quote Link to comment https://forums.phpfreaks.com/topic/202949-opposite-of-distinct-without-group-by/ Share on other sites More sharing options...
ignace Posted May 26, 2010 Share Posted May 26, 2010 SELECT * FROM table GROUP BY group_id, contact_id HAVING count(*) > 1 Quote Link to comment https://forums.phpfreaks.com/topic/202949-opposite-of-distinct-without-group-by/#findComment-1063537 Share on other sites More sharing options...
sw9 Posted May 26, 2010 Author Share Posted May 26, 2010 thanks for that suggestion. there had to be a dupe of the same table to really get what was needed. i ended up using this query: SELECT gc.* FROM group_contact gc INNER JOIN ( SELECT id, group_id, contact_id FROM group_contact GROUP BY group_id, contact_id HAVING count(*) > 1 ) dup_gc ON ( gc.group_id = dup_gc.group_id AND gc.contact_id = dup_gc.contact_id AND gc.id <> dup_gc.id ) Quote Link to comment https://forums.phpfreaks.com/topic/202949-opposite-of-distinct-without-group-by/#findComment-1063864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.