• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL wizard needed: join two tables without excluding "missing" records from right-hand table

#2
[eluser]aidehua[/eluser]
Funny how taking the time to write out the question in this forum can sometimes help you see the light...

I've cracked it (I think). This seems to be doing what I want:

Code:
SELECT tbProducts.id, tbProducts.name, tbProduct_images.img_name
FROM tbProducts
LEFT JOIN tbProduct_images
ON tbProducts.id = tbProduct_images.prod_id
AND tbProduct_images.is_primary = TRUE

i.e. the qualifier "tbProduct_images.is_primary = TRUE" goes in the ON part of the JOIN, and not in a WHERE clause at the end.

Any SQL Kings or Queens out there care to comment on whether this is the right way to go about it?

Oh - and a follow-up question for a bonus point: what if I decide that I want to get the primary image for each product, if there is one, or, if there isn't one, then just get the first image in tbProduct_images for that particular image. Can I do that in a single query?


Messages In This Thread
SQL wizard needed: join two tables without excluding "missing" records from right-hand table - by El Forum - 09-06-2010, 01:25 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.