我有以下,工作MySQL查询:条件JOIN语句在MySQL
SELECT
a.id id,
a.price price,
a.stock stock,
a.max_per_user max_per_user,
a.purchased purchased,
b.quantity owned
FROM
shop_items a
JOIN shop_inventory b
ON b.iid=a.id
AND b.cid=a.cid
WHERE
a.cid=1
AND a.szbid=0
AND a.id IN(3,4)
的JOIN
加入表shop_inventory b
返回b.quantity owned
。但是,如果shop_inventory b
表中没有记录,那么b.iid=a.id
我希望它返回b.quantity = 0
。我将如何做到这一点?
+1为'COALESCE'。 –
谢谢,非常感谢:) –
谢谢,我已经纳入托马斯的'GROUP BY'的想法,并总结个别数量,并使用'COALESCE'为'NULL'值:)。 –