我有问题链接两个表,其中一个没有价值,它不显示。为了简化这个过程,我创建了一个临时表来显示:
细节
ps_product table
product_id name
1 Product 1
2 Product 2
3 Product 3
需要与另一个表链接以显示图像路径的:
ps_product_details table
details_id product_id properties value
1 1 image_location 1.jpg
2 1 width 10 cm
3 1 height 5 cm
4 2 image_location 2.jpg
5 3 width 9 cm
6 3 height 5 cm
在这里我可以和他们一起 LEFT JOIN
:
SELECT * FROM ps_product
LEFT JOIN ps_product_details ON ps_product.id = ps_product_details.product_id
结果是:
product_id name details_id product_id properties values
1 Product 1 1 1 image_location 1.jpg
1 Product 1 2 1 width 10 cm
1 Product 1 3 1 height 5 cm
2 Product 2 4 2 image_location 2.jpg
3 Product 3 5 3 width 9 cm
3 Product 3 6 3 height 5 cm
到目前为止没有问题。
问题
我不想展示 width
以及 height
在加入的过程中,我能做些什么 WHERE properties = 'image_location'
,但当我把它放进去时,它显示了:
product_id name details_id product_id properties values
1 Product 1 1 1 image_location 1.jpg
2 Product 2 4 2 image_location 2.jpg
如图所示,产品3消失是因为它没有图像,我不想显示产品3不存在,它只是没有图像。
我真正想要的是:
product_id name details_id product_id properties values
1 Product 1 1 1 image_location 1.jpg
2 Product 2 4 2 image_location 2.jpg
3 Product 3 null 3 image_location null
这个 details_id
以及 values
可以为null,因为我不知道它的值。但是 properties
我已经知道这是一个 image_location
. 这样我就可以不显示图片而不显示产品。
我试过的
从@shree.pat18这里
这是我从中实现的查询:
SELECT * FROM ps_product
LEFT JOIN ps_product_details ON ps_product.id = ps_product_details.product_id
where (properties = 'image_location') or (properties = properties)
其结果与前面的查询相同:
SELECT * FROM ps_product
LEFT JOIN ps_product_details ON ps_product.id = ps_product_details.product_id
有没有解决办法,谢谢你的时间。
1条答案
按热度按时间6ioyuze21#
将条件放入
ON
子句而不是WHERE
条款:如果你真的想挑剔,你可以
coalesce()
在SELECT
条款: