i have a query and for some reason left join is not working and i don't know why. i need left join because maybe some products don't have an image. But when i use left join no products are displayed, it works only with inner join. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.id_product,
p.price,
pig.image
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.name = :id OR c2.name = :id OR c3.name = :id )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.id_product
LIMIT $start, $row_limit");
$stmt->bindParam(":id",$id);
$stmt->execute();
so i want INNER JOIN tbl_products_images_gallery pig
replaced with LEFT JOIN tbl_products_images_gallery pig
obviously an inner join will not produce the same query result as a left join- but I bet you know that already Chris.
Let me point to another -less apparent- reason that your left joint may not work as expected. A.Check if your Database/table fields(columns) have specific constrains. For instance non-nullable fields. B.If such constrain exists on a field (used to join 2 tables)then doing a left joint that is expected to produce null records will simply not going to return null records. This is because this is set in the constrain criteria of your DB/Schema. C.If you run a simple Desc 'Schema.Table' command should expose you the field constrains.
Hope this helps