我使用以下查询:
SELECT shop_entrys.id, shop_images.path FROM shop_entrys,shop_images
WHERE shop_entrys.id = shop_images.s_id AND
shop_images.pos = 0 AND
shop_entrys.category_id = 1
ORDER BY shop_entrys.pos ASC
然而,SuffiTeNeCes中的一行可能与Sujl图像中的行没有链接。因此。。。WHERE shop_entrys.id=商店图像s_id。。。不会被满足的。在这种情况下,我还是想返回一个结果。例如:
shop_entrys.id shop_images.path 1 "/img1.jpg" ... ... 42 "not found"
如何更改上述查询以仍然返回结果?
最佳答案
使用带有COALESCE的左外部联接为第二列提供默认值
SELECT shop_entrys.id, COALESCE(shop_images.path, 'NOT FOUND' )
FROM shop_entrys
LEFT OUTER JOIN shop_images
ON shop_entrys.id = shop_images.s_id AND shop_images.pos = 0
WHERE shop_entrys.category_id = 1
ORDER BY shop_entrys.pos ASC
关于mysql - 返回值,即使一个条目不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40867653/