【发布时间】:2014-05-24 11:32:28
【问题描述】:
我有桌子:
+------------+
| Ingredient |
+------------+
| id |
+------------+
| name |
+------------+
+---------------+
| Relingredient |
+---------------+
| id_ingredient |
+---------------+
| id_recipe |
+---------------+
+--------+
| Recipe |
+--------+
| id |
+--------+
| name |
+--------+
并且我需要选择具有我想要的成分的食谱(所有成分都传递给他们)我试过这个:
SELECT R.id, R.nom FROM Recipe R, Relingredient RI, Ingredient I
WHERE R.id = RI.id_recipe AND RI.id_ingredient = I.id AND I.name='onion' AND I.name='oil'
GROUP BY R.name
但返回零行
我也试过这个:
SELECT R.id, R.nom FROM Recipe R, Relingredient RI, Ingredient I
WHERE R.id = RI.id_recipe AND RI.id_ingredient = I.id AND (I.name='onion' or I.name='oil')
GROUP BY R.name
但它会选择所有含有洋葱或油的食谱,而不仅仅是那些含有洋葱和油的食谱......我该怎么办?
(编辑)我想要的示例:
例如我有食谱:
1:烤鸡(配料:鸡肉、洋葱、油)
2:中国汤(配料:猪肉,洋葱,油,面条)
3:蔬菜三明治(配料:面包、油、番茄、沙拉)
查询应该只返回食谱:烤鸡和中国汤
谢谢你帮助我!!
【问题讨论】:
标签: sql select many-to-many