Shadowing Posted February 19, 2013 Share Posted February 19, 2013 Hey guys I need help with a select statement I can't figure out I'm wanting to select users.id that does not match game.id where users.computer_player = 1 I tried this below which gives me 9 duplicate id's for every id selected. Can't figure out why. hoping someone can help me out. users.id and game.id are both unique index's SELECT users.id FROM users LEFT JOIN game ON users.id != game.id WHERE users.computer_player = 1 results from this query id 6876 6876 6876 6876 6876 6876 6876 6876 6876 6877 6877 6877 6877 6877 6877 6877 6877 6877 6878 6878 6878 6878 6878 6878 6878 6878 6878 6879 6879 6879 Quote Link to comment https://forums.phpfreaks.com/topic/274706-selecting-a-column-with-left-join/ Share on other sites More sharing options...
jazzman1 Posted February 19, 2013 Share Posted February 19, 2013 Did you try GROUP BY users.id ? Quote Link to comment https://forums.phpfreaks.com/topic/274706-selecting-a-column-with-left-join/#findComment-1413526 Share on other sites More sharing options...
Psycho Posted February 20, 2013 Share Posted February 20, 2013 A little explanation of what the relationship between tables is would be helpful. I don't know if there can be multiple associated records in the game table for each user. But, it seems you are wanting to find the users that have NO associated records in the game table. If so, then use this: SELECT users.id FROM users LEFT JOIN game ON users.id == game.id WHERE users.computer_player IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/274706-selecting-a-column-with-left-join/#findComment-1413532 Share on other sites More sharing options...
Shadowing Posted February 20, 2013 Author Share Posted February 20, 2013 Thanks for the replies guys I got this to work using jazzman's route SELECT users.id FROM users LEFT JOIN game ON users.id != game.id WHERE users.computer_player = 1 GROUP By users.id pyscho return empty query. columns for game.id is unique and users.id is unique. So no multiple associations. The game table either has that id in it or it doesnt. Even though group by works so anyways my test didnt test to make sure this part works users.id != game.id but left joining it like that should only return results if there are no matches right? I'll double check here in a bit. Quote Link to comment https://forums.phpfreaks.com/topic/274706-selecting-a-column-with-left-join/#findComment-1413561 Share on other sites More sharing options...
Barand Posted February 20, 2013 Share Posted February 20, 2013 (edited) SELECT users.id FROM users LEFT JOIN game ON users.id = game.id WHERE game.id IS NULL If there is no match it is the fields from game that will be null Edited February 20, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274706-selecting-a-column-with-left-join/#findComment-1413601 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.