【发布时间】:2016-10-03 18:13:57
【问题描述】:
我正在构建一个按成分搜索食谱的应用。我有 5 张桌子:
食谱
RecipeId | RecipeTitle | Directions | PrepTime | CookTime
成分
IngredientId | IngredientName
类别
CategoryId | CategoryName
RecipeIngredient 联结表(多对多)
RecipeId | IngredientId | Quantity | UOM | Comments
RecipeCategory 联结表(多对多)
RecipeId | CategoryId
我想做的是让用户选择成分、类别和时间,应用程序将搜索适合用户输入的食谱。在我的应用程序中实现之前,我正在测试它如何使用 DB Browser for SQLite 工作,但它没有按预期工作。
这是我尝试过的最新查询(当然我假设用户在这里输入,他们不受他们可以选择的成分和类别数量的限制):
SELECT RecipeTitle, COUNT(RecipeTitle) AS Ing FROM Recipe
JOIN RecipeIngredient ON Recipe.RecipeId = RecipeIngredient.RecipeId
JOIN RecipeCategory ON Recipe.RecipeId = RecipeCategory.RecipeId
WHERE
RecipeIngredient.IngredientId =
(SELECT Ingredient.IngredientId FROM Ingredient
WHERE Ingredient.IngredientName = "olive oil")
OR
RecipeIngredient.IngredientId =
(SELECT Ingredient.IngredientId FROM Ingredient
WHERE Ingredient.IngredientName = "beef")
OR
RecipeIngredient.IngredientId =
(SELECT Ingredient.IngredientId FROM Ingredient
WHERE Ingredient.IngredientName = "parsley")
AND
RecipeCategory.CategoryId =
(SELECT Category.CategoryId FROM Category
WHERE Category.CategoryName = "Dinner")
AND
Recipe.PrepTime + Recipe.CookTime < 35 -- In minutes
GROUP BY RecipeTitle
ORDER BY Ing DESC -- Orders results by recipes that have the most out of the ingredients the user chose, with less relevant recipes at the bottom
以下是我遇到的问题:
- 我希望
COUNT(RecipeTitle)计算结果中每个食谱中选择的成分的数量,但它也计算类别。 - 类别和时间搜索不起作用。它确实显示了包含所选成分的食谱,但也显示了不是
Dinner并且需要总时间为 35 分钟或以上的食谱。
【问题讨论】: