I am trying to write a query that will pull some product information AND photos, if the product has any.
select
prod.pID, prod.Manufacturer_Name, prod.pUPC, prod.pNum, prod.pPrice, prod.pSalesPrice, prod.pSalesDate, prod.pSalesEndDate, prod.pSale,
GROUP_CONCAT(photoName) as photos
from
ds_products as prod
inner join ds_photos as pics on pics.objectID=prod.pID
where
pics.photoFlag =2
group by
prod.pID
The problem with this is that products that do not have photos get left out of the result set. What do I need to add and/or change to allow for a product that doesn’t appear in the photos table to show up in the results?
Thanks
EDIT
I tried the LEFT JOIN instead of inner but get the same result set. If i run just:
select
prod.pID, prod.Manufacturer_Name, prod.pUPC, prod.pNum, prod.pPrice, prod.pSalesPrice, prod.pSalesDate, prod.pSalesEndDate, prod.pSale
from
ds_products as prod
I get about 600k results. The inner and or left join queries get about 190k results. Is there another way to do this?
Use outer join
I have added a condition which check if photoFlag is null for the products which don’t have corresponding pics
Here is working fiddle : http://sqlfiddle.com/#!2/c2a9c/1/0
For more information: http://dev.mysql.com/doc/refman/5.0/en/outer-join-simplification.html