假设我有一个包含3列的表格,如下所示
------------------------------
|id | recipe_id | sub_category |
------------------------------
| 1 | 80 | 2 |
| 2 | 80 | 6 |
| 3 | 80 | 3 |
| 4 | 82 | 1 |
| 5 | 83 | 2 |
| 6 | 85 | 5 |
| 7 | 80 | 10 |
------------------------------
如何获得包含子类别2和6的食谱ID? 是否可以使用mysql查询获取它?
答案 0 :(得分:1)
试试这个:
SELECT recipe_id
FROM mytable
WHERE sub_category IN (2, 6)
GROUP BY recipe_id
HAVING COUNT(DISTINCT sub_category) = 2