0

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;
4

1 回答 1

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;

请参阅带有演示的 SQL Fiddle

于 2013-04-04T11:55:54.137 回答