我有两个表:
查询:
SELECT p.id, p.name, p.price, pr.price AS newPrice, pr.id AS prID
FROM products p
LEFT JOIN prices pr ON pr.product_id = p.id
WHERE pr.group_id = '1'
ORDER BY p.id ASC
我需要查看所有产品,如果价格已满,则需要从价格表中获取结果。 但是当我写“ WHERE”时,我只能看到填充的数据
对不起,我的英语
答案 0 :(得分:3)
使用LEFT JOIN
时,prices
表上的过滤器应放在ON
子句中
SELECT p.id, p.name, p.price, pr.price AS newPrice, pr.id AS prID
FROM products p
LEFT JOIN prices pr ON pr.product_id = p.id AND pr.group_id = 1
ORDER BY p.id ASC