0
我正在構建一個按配料搜索食譜的應用程序。我有5個表:加入三個表,查詢不按預期在SQLite中工作
配方
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分鐘或更長時間的總時間。
有人告訴我有作爲'Category.CategoryName'沒有這樣的列,但後來我加入了'Category'表,它的工作吧!謝謝。但我有個問題。加入4張桌子可以嗎?這是不錯的做法嗎? – user3501779
是的,絕對。複雜的查詢連接許多表。 –
好的,謝謝你的幫助。我應該加入分類表格,然後像我一樣,對嗎? – user3501779