SELECT id WHERE

时间:2015-08-01 13:52:00

标签: mysql select where

我有表:

product_id | filter_id
        68 | 2
        68 | 4
        66 | 1
        69 | 4
        67 | 1
        67 | 5

我想要

`product_id` WHERE `filter_id` = '2' AND `filter_id` = '4'

即,我只需要获得product_id = 68

SELECT `product_id` FROM `filter` WHERE `filter_id` IN (2,4) - unsuitable

我该怎么做?

2 个答案:

答案 0 :(得分:5)

您可以使用group by选择同时包含filter_id' s 2和4的所有product_id

select product_id
from mytable
where filter_id in (2,4)
group by product_id
having count(*) = 2 

如果(product_id, filter_id)不唯一,请使用count(distinct filter_id) = 2确保产品同时包含filter_id&#39>

答案 1 :(得分:3)

你可以这样做:

{{1}}

结果:

{{1}}

结果project structure