我希望产品在IN
子句中包含所有选项。这是我的SQL:
SELECT
p.title, p.price FROM products AS p, product_options AS po
WHERE
p.product_id = po.product_id
AND
po.product_option_id IN (5,1,38,39)
GROUP BY
p.product_id;
答案 0 :(得分:2)
如果要返回包含所有这些选项的结果,则可以使用:
SELECT p.title, p.price
FROM products AS p
INNER JOIN product_options AS po
ON p.product_id = po.product_id
WHERE po.product_option_id IN (5,1,38,39)
GROUP BY p.product_id
HAVING COUNT(DISTINCT po.product_option_id) = 4;