947740 Posted May 12, 2010 Share Posted May 12, 2010 I'm trying to use this query: SELECT prizes.ID, prizes.name, prizes.value, prizes.points, SUM(bids.points) FROM prizes left join (bids) on (prizes.ID = bids.prize_id) to pull data from the database. Whenever I run that, though, I only get 1 row of data (the first one from the prizes table)...I want all the rows of data. My MySQL version is 5.0.86. Here are the tables prizes and bids: prizes ID SERIAL name varchar(100) value int(4) points int(5) bids ID SERIAL prize_id int(5) uname varchar(50) points int(5) Data in prizes: ID name value points 3 Amazon.com! Yeah, that\'s right! THE WEBSITE HAHAH... 2147483647 2147483647 2 test prize 7 700 4 Creative Zen Stone 4GB 65 6500 Data in bids: ID prize_id uname points 1 2 |m.t.w|kana 350 2 2 |m.t.w|kana 75 3 2 |m.t.w| hunted 57 4 2 |M.T.W|Kana 218 It's my first time trying to join tables...so it might be something simple. Thanks ahead of time. Quote Link to comment https://forums.phpfreaks.com/topic/201449-cant-get-left-join-to-work/ Share on other sites More sharing options...
luca200 Posted May 12, 2010 Share Posted May 12, 2010 you should add GROUP BY prizes.ID at the end of the query Quote Link to comment https://forums.phpfreaks.com/topic/201449-cant-get-left-join-to-work/#findComment-1057200 Share on other sites More sharing options...
947740 Posted May 12, 2010 Author Share Posted May 12, 2010 you should add GROUP BY prizes.ID at the end of the query xD I did that, and it didn't work. But then I entered a blank bid of 0 points in for each prize, and then it showed up. So it was only pulling a row of prize data when it had a bid of at least 0 points. Weird, but an easy thing to fix. Thanks for the response! My final query: SELECT prizes.ID as ID, prizes.name as name, prizes.value as value, prizes.points as points, SUM(bids.points) as total FROM prizes join bids on prizes.ID = bids.prize_id GROUP BY prizes.ID Quote Link to comment https://forums.phpfreaks.com/topic/201449-cant-get-left-join-to-work/#findComment-1057298 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.