左连接不显示产品

时间:2017-03-06 09:58:57

标签: php mysql sql pdo

我有一个查询,由于某种原因,左连接不起作用,我不知道为什么。我需要左连接,因为可能有些产品没有图像。但是当我使用左连接时,没有显示任何产品,它只适用于内连接。这是我的疑问:

$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();

所以我希望INNER JOIN tbl_products_images_gallery pig替换为LEFT JOIN tbl_products_images_gallery pig

1 个答案:

答案 0 :(得分:0)

很明显,内部联接不会产生与左联接相同的查询结果-但我敢打赌,您知道克里斯已经。

让我指出另一个不太明显的原因,即您的左关节可能无法按预期工作。 答:检查您的数据库/表字段(列)是否具有特定的约束。例如非空字段。 B.如果这样的约束存在于一个字段(用于联接2个表)上,那么做一个预期会产生空记录的左关节将根本不会返回空记录。这是因为这是在DB / Schema的约束条件中设置的。 C.如果您运行一个简单的Desc'Schema.Table'命令,则会暴露出字段约束。

希望这会有所帮助

相关问题