我正在寻找一种解决方案,让我无法遍历每条记录,检查所有子记录是否符合所需条件。
使用其他SQL语言,我可以使用ALL进行查询,但sqlite不支持 “where field = ALL(选择子查询)”语法。
我有3张桌子。一,食谱清单。二,配方成分清单。三,用户将打开和关闭值以选择正确的配方的表格。
我需要显示包含“ALL”用户检查的成分的所有食谱。
因此,如果用户选择“green pepers”和“chicken”,则只会显示包含BOTH的食谱。
Table 1: Recipes
recipe_id int
recipe_name text
Table 2: Ingredients
recipe_header int ( link to header)
recipe_ingredient text
Table 3: User selection ( this table will be modified on the fly to represent only ingredients that are included in recipes as different ingredients are checked off)
recipe_ingredient text
user_selected text (true/false)
Table 1:
t1_id t1_name
--------- ---------------
1 Chicken Marsala
2 Chicken Parmesan
3 Flank Steak
4 Grilled Salmon
Table 2:
t2_link t2_ingredient
------- -------------
1 chicken
1 green peppers
1 olive oil
2 chicken
2 olive oil
2 peeled tomatoes
3 flank steak
3 olive oil
3 soy sauce
3 pepper
4 salmon fillet
4 soy sauce
4 pepper
Table 3:
t3_ingredient t3_checked
------------- ----------
chicken ( true or false )
flank steak ( true or false )
green peppers ( true or false )
olive oil ( true or false )
peeled tomatoes ( true or false )
pepper ( true or false )
salmon fillet ( true or false )
soy sauce ( true or false )
如果用户选择以下成分(true) “鸡肉”,“橄榄油”
- 鸡肉马沙拉&将展示鸡巴马干酪。
如果用户选择“酱油”和“辣椒”
- 侧翼斯坦&将展示烤三文鱼。
如果用户选择“橄榄油”
- 马尔萨拉鸡肉,鸡肉巴马干酪&将展示Flank Steak。
答案 0 :(得分:0)
在this SQL小提琴:
中演示了其中一种方法select * from Table1
where t1_id IN (
SELECT t2_link
FROM Table2 INNER JOIN Table3 ON
t2_ingredient = t3_ingredient AND t3_checked = 'true'
GROUP BY t2_link
HAVING COUNT(DISTINCT t2_ingredient) = (
SELECT COUNT(t3_ingredient) FROM Table3 WHERE t3_checked = 'true'
)
)
“鸡肉”,“橄榄油”的结果
| t1_id | t1_name |
|-------|------------------|
| 1 | Chicken Marsala |
| 2 | Chicken Parmesan |