简体   繁体   English

联接三个表,查询无法在SQLite中按预期方式工作

[英]JOINing three tables, query not working as expected in SQLite

I'm building an app that searches for recipes by ingredients. 我正在构建一个按成分搜索配方的应用程序。 I have 5 tables: 我有5张桌子:

Recipe 食谱

RecipeId | RecipeTitle | Directions | PrepTime | CookTime

Ingredient 成分

IngredientId | IngredientName

Category 类别

CategoryId | CategoryName

RecipeIngredient junction table (many-to-many) 配方成分表(多对多)

RecipeId | IngredientId | Quantity | UOM | Comments

RecipeCategory junction table (many-to-many) RecipeCategory联结表(多对多)

RecipeId | CategoryId

What I want to do is let the user choose the ingredients, categories, and time and the app will search for recipes fitting the user's input. 我要做的是让用户选择配料,类别和时间,然后该应用将搜索适合用户输入的食谱。 I'm testing how this would work using DB Browser for SQLite before implementing it in my app, but it isn't working as expected. 我正在测试在我的应用程序中实现它之前如何使用DB Browser for SQLite来工作,但是它没有按预期工作。

Here's the latest query I've tried (of course I'm assuming user input here, and they aren't limited by the number of ingredients and categories they can choose): 这是我尝试过的最新查询(当然,我在这里假设用户输入,并且不受限于可以选择的成分和类别的数量):

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

Here are the problems I'm having: 这是我遇到的问题:

  • I want COUNT(RecipeTitle) to count how many of the ingredients chosen are in every recipe in the results, but instead it's also counting the category as well. 我希望COUNT(RecipeTitle)可以计算结果中每个配方中所选成分的数量,但是相反,它也要计算类别。
  • Category and time search aren't working. 类别和时间搜索无效。 It's indeed displaying recipes that contain the chosen ingredients, but also displaying ones that aren't Dinner and that require a total time of 35 minutes or over. 它的确在显示包含所选成分的食谱,而且还显示非Dinner且需要总共35分钟或更长时间的食谱。

Try 尝试

SELECT RecipeTitle, COUNT(Ingredient.IngredientId) AS Ing 
FROM Recipe
JOIN RecipeIngredient ON Recipe.RecipeId = RecipeIngredient.RecipeId
JOIN RecipeCategory ON Recipe.RecipeId = RecipeCategory.RecipeId
JOIN Ingredient ON RecipeIngredient.IngredientId = Ingredient.IngredientId 
JOIN Category ON CategoryCategoryId = RecipeCategory.CategoryId
WHERE Ingredient.IngredientName IN ('olive oil', 'beef', 'parsley')
AND Category.CategoryName = 'Dinner'
AND (Recipe.PrepTime + Recipe.CookTime) < 35
GROUP BY RecipeTitle
ORDER BY Ing DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM