Jump to content

Can't get left join to work


947740

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/201449-cant-get-left-join-to-work/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.