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 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. 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 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? 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 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 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)) 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 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! 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
Archived
This topic is now archived and is closed to further replies.