bulgin Posted April 17, 2009 Share Posted April 17, 2009 I know this is basic stuff but I'm stumped and starting to learn. I have one table, users and it contains a groupid. In all cases users are listed in groupid = 2 or groupid = 4. Sometimes they are listed in both groupid 2 AND groupid 4. Why doesn't this work? SELECT * FROM `users` WHERE groupid = 2 and groupid = 4; I just want to find those users who are in both groups, 2 and 4. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/ Share on other sites More sharing options...
jackpf Posted April 17, 2009 Share Posted April 17, 2009 You want an OR. Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812342 Share on other sites More sharing options...
bulgin Posted April 17, 2009 Author Share Posted April 17, 2009 I tried an OR and it shows ALL users, not just those who are in BOTH groupid Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812349 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 What are the other columns in the table? Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812364 Share on other sites More sharing options...
bulgin Posted April 17, 2009 Author Share Posted April 17, 2009 table users: Field Type Null Default Comments userid int(11) No groupid int(11) No entered datetime Yes NULL modified timestamp No CURRENT_TIMESTAMP Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812382 Share on other sites More sharing options...
kickstart Posted April 17, 2009 Share Posted April 17, 2009 Hi Do I take it that a user could have 2 rows, one for each groupid (ie, userid is not the unique key)? If so then give this a try:- SELECT * FROM users a JOIN users b ON a.userid = b.userid WHERE a.groupid = 2 AND b.groupid = 4 All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812395 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 SELECT * FROM users WHERE (user_id IN (SELECT user_id FROM users WHERE groupid=2) AND user_id IN (SELECT user_id FROM users WHERE groupid=4)) Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812400 Share on other sites More sharing options...
gurroa Posted April 17, 2009 Share Posted April 17, 2009 How about this? SELECT * FROM users where groupid = 2 or groupid = 4 group by userid Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812433 Share on other sites More sharing options...
bulgin Posted April 18, 2009 Author Share Posted April 18, 2009 Hi. They all worked but mtoynbee's gave me exactly what I wanted. Thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/154499-solved-simple-duplicate-query-not-working/#findComment-812812 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.