簡體   English   中英

查詢以將表中的行與數字列表進行匹配,按匹配的行數進行排序,並與標簽進行匹配,是否一樣?

[英]Query to match rows in a table against a list of numbers, order by the amount of rows matched, and match against tags and do the same?

我有一個食譜數據庫。 在此數據庫中,我有5個表:

- recipes
- ingredients
- ingredients_available
- tags
- tags_available

具有以下結構:

- recipes
id, name
- ingredients
id, recipe_id, ingredient_id
- ingredients_available
id, name
- tags
id, recipe_id, tag_id
- tags_available
id, name

我想用一組成分查詢數據庫(它將在其中有編號),例如5、2、3,並找到部分或公正地匹配此編號的配方。 然后,我想按匹配的配料量訂購食譜。 我還想知道完成食譜需要其他哪些成分,以及是否有相應的Ingredient_id。 我還希望在查詢中添加無限數量的標簽,因此只能找到與標簽匹配的Ingredient_id,然后再次按匹配的標簽數量進行排序。

我將如何創建這樣的查詢,也可以通過一個查詢來完成嗎? 我正在使用MySQL數據庫。 任何指向正確方向的指針都將是很好的。 謝謝。

我認為“加入”的力量就在這里。 我不確定要產生什么可能的組合,但是以下是我想到的一些與您在此處呈現的結構有關的場景:

//獲取配方原料

Select ingredients.* from recipe recipe
Inner Join ingredients ingredients on recipe.id = ingredients.recipe_id
order by ingredients.id

//獲取配方的可用成分

Select ingredients.* from recipe recipe
Inner Join ingredients ingredients on recipe.id = ingredients.recipe_id
Inner Join ingredients_available ingredients_available on ingredients.ingredient_id = 
ingredients_available.id
order by ingredients.id

這是mysql的'join'概念的鏈接,用於解決您的問題:

http://dev.mysql.com/doc/refman/5.0/en/join.html

干杯!

我設法通過以下查詢完成了問題的第一部分:

SELECT recipes.*, COUNT(ingredients.id) AS occurences FROM recipes INNER JOIN ingredients ON recipes.id = ingredients.recipe_id WHERE ingredient_id IN (1, 3) GROUP BY recipes.id ORDER BY occurences DESC;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM