现在我在ios应用程序中使用sqlite,我希望能够搜索可以从成分列表中制作的食谱(即作为所提供成分的子集的食谱)
例如:
Recipe 1: A B C
Recipe 2: A B
Recipe 3: C D
Recipe 4: A
Recipe 5: E
Query for ingredients A B C returns recipes {1, 2, 4}
Query for ingredients A B returns recipes {2, 4}
Query for ingredients D returns {}
目前我已经设置的是
Table Items
name text primary key
Table Recipes
ID required_item integer, recipe_name text
我可以轻松地查询包含三种成分中任何一种的食谱,并查询包含所有三种成分的食谱,但我无法弄清楚如何只查询
Recipes ∈ P(Ingredients)
抱歉,我是数据库编程新手,任何帮助将不胜感激
你可以使用left join
到搜索表。然后group by
食谱。用一个having
子句要求对于每个食谱,没有不在搜索列表中的成分。例如:
select ri.RecipeNr
from RecipeIngredients ri
left join
(
select 'A' as Ingredient
union all
select 'B'
union all
select 'C'
) search
on ri.Ingredient = search.Ingredient
group by
ri.RecipeNr
having count(case when search.Ingredient is null then 1 end) = 0
SQL Fiddle 的实时示例。 http://sqlfiddle.com/#!5/aa772/29/0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)