我想选择recipe_id作为具有我的两种成分的食谱但我不能使用AND这样(我的意思是它为所有列返回null):
SELECT
vsearch.ingredient_id,
vsearch.ingredtypes_id,
vsearch.name,
vsearch.recipe_id,
vsearch.qingred
FROM
vsearch
WHERE
vsearch.name = 'olive' AND vsearch.name = 'apple'
vsearch是一个视图:
SELECT
ingredient.ingredient_id,
ingredient.ingredtypes_id,
ingredient.name,
quantity.recipe_id,
quantity.ingredient_id AS qingred
FROM
ingredient ,
quantity
WHERE
strong textingredient.ingredient_id = quantity.ingredient_id
继承我的数据库表:
答案 0 :(得分:1)
查找(例如)查找包含黄瓜和番茄的食谱ID;
SELECT recipe_id
FROM vsearch
WHERE name = 'cucumber' OR name = 'tomato'
GROUP BY recipe_id
HAVING COUNT(*) = 2
...其中2是必须匹配的成分的名称(在这种情况下都是)
答案 1 :(得分:1)
select *
from vsearch
where recipe_id
in
( select recipe_id from vsearch where name = 'apple'
intersect
select recipe_id from vsearch where name = 'olive'
)