需要获取具有计数2的行

时间:2016-05-22 07:59:25

标签: mysql sql

我有表A如        TABLE1

  =======================
  | id | product_id | filter_id | category_id |
  | 16 | 33         | 6         | Null        |
  | 23 | 40         | 16        | 76          |
  | 48 | 20         | 6         | 45          |
  | 69 | 10         | 6         | 87          |
  | 70 | 10         | 9         | 67          |

现在我必须找到product_id 10,其中包含filter_id 6和9

2 个答案:

答案 0 :(得分:0)

您可以使用having

select 
    product_id, count(product_id) 
from 
    teble1 
group by
    product_id
having
    count(product_id) = 2;

答案 1 :(得分:0)

使用其中一个样本

SELECT *
FROM TABLE1
WHERE products_id = 10
AND filter_id IN (6,9);

OR

SELECT *
FROM TABLE1
WHERE products_id = 10
AND (filter_id = 6 OR filter_id = 9);