burhankhan Posted May 6, 2007 Share Posted May 6, 2007 Hi: I have two tables as: Members ---------------------------- MEMBER_ID | NAME ----------------------------- 1 abc 2 kdjf 3 kek GROUP ------------------------- MEMBER_ID | GROUP_ID ------------------------- 3 1 Now i want to fetch records from first table, where members.member_id != groups.members_id I use many types of quires and it is working: SELECT * FROM `members` RIGHT OUTER JOIN `member-group` ON `member-group`.memberid != members.memberid SELECT * FROM `members` WHERE `id` != (SELECT `memberid` FROM `groups`) Also left join working... But when second table "GROUP" is empty, then no query is working. It fetch ZERO records. What is its solution? Quote Link to comment https://forums.phpfreaks.com/topic/50229-joins-problem/ Share on other sites More sharing options...
fenway Posted May 6, 2007 Share Posted May 6, 2007 Ugh... != is evil. If you want to find non-matching rows, use a LEFT JOIN with the FK constraint in the ON clause and use IS NULL in the where clause. Quote Link to comment https://forums.phpfreaks.com/topic/50229-joins-problem/#findComment-246652 Share on other sites More sharing options...
burhankhan Posted May 6, 2007 Author Share Posted May 6, 2007 I couldn't understand what you said....... I use left join as: SELECT * FROM `members` LEFT JOIN `member-group` ON `member-group`.memberid IS NULL But still not working... Quote Link to comment https://forums.phpfreaks.com/topic/50229-joins-problem/#findComment-246690 Share on other sites More sharing options...
bubblegum.anarchy Posted May 7, 2007 Share Posted May 7, 2007 SELECT * FROM members LEFT JOIN member-group ON members.member_id = member-group.member_id WHERE member-group.member_id IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/50229-joins-problem/#findComment-246932 Share on other sites More sharing options...
fenway Posted May 7, 2007 Share Posted May 7, 2007 SELECT * FROM members LEFT JOIN member-group ON members.member_id = member-group.member_id WHERE member-group.member_id IS NULL Yeah, I guess I should have spelled that out... my bad. Quote Link to comment https://forums.phpfreaks.com/topic/50229-joins-problem/#findComment-247271 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.