根据MYSQL中用户选择的选项获取产品

时间:2013-04-04 11:54:00

标签: mysql

我希望产品在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;

1 个答案:

答案 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;

请参阅SQL Fiddle with Demo