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所拥有的.但是,如果shop_inventory b表中没有记录,其中b.iid = a.id我希望它返回b.quantity = 0.我该怎么做?
最佳答案
请改用LEFT JOIN.和COALESCE因为一些记录为null(我猜).尝试,
SELECT a.id id,COALESCE(b.quantity,0) owned
FROM shop_items a
LEFT 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)