简体   繁体   English

从给定的原料中找出可能的食谱数量? SQL查询

[英]Find the number of recipes possible from given ingredients? SQL Query

I'm struggling to figure out the query (drawing a blank really) to get all recipes that can be created from the stock of ingredients available.我正在努力找出查询(实际上是画一个空白)以获取可以从可用成分库存中创建的所有食谱。

Tables that are created are:创建的表有:

CREATE TABLE recipe(id int, name varchar(25));  
CREATE TABLE ingredients(id int, name varchar(25), stock int);
CREATE TABLE recipeingredients(recipe_id int, ingredients.id int, amount int);

Records in the database would be something like this数据库中的记录将是这样的

Recipe
ID | Name        |
1  | Brown Bread |
2  | White Bread |

Ingredients
ID | Name        | Stock |
1  | White Flour | 2     |
2  | Wheat Flour | 1     |
3  | Yeast       | 17    |
4  | Water       | 12    |

RecipeIngredients
RecipeID | IngredientID | Amount |
1        | 1            | 1      |
1        | 3            | 4      |
1        | 4            | 8      |
2        | 2            | 1      |
2        | 3            | 4      |
2        | 4            | 8      |

So the result would be something like this所以结果会是这样的

Name        | Count |
White Bread | 1     |
Wheat Bread | 1     |

This might be something simple but am very rusty at SQL now.这可能很简单,但现在我对 SQL 非常生疏。

select recipe
, min([count]) as 'count'

from (
    select r.name as recipe
    , i.name as ingredient
    , i.stock / ri.amount as 'count'

    from recipeingredients ri
      inner join ingredients i on i.id = ri.ingredients_id
      inner join recipe r on r.id = ri.recipe_id
) q

group by recipe

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

相关问题 SQL - 从 MySQL 数据库中选择共享相似数量成分的食谱? - SQL - Select recipes that share similar number ingredients from MySQL database? SQL查询可从成分集合中制成的配方 - SQL query for recipes that can be made from collection of ingredients 多对多查询查找带有XX成分的所有“配方” - Many to Many query Find all “recipes” with XX ingredients 仅返回包含所有成分的食谱 Sql - Return only recipes that contains all ingredients Sql SQL - 查询以查找给定类别中的条目数 - SQL - Query to find the number of entries in a given category SQL查询以查找给定一年用户参加的最大事件数 - SQL query to find maximum number of attended events by user given a year SQL 查询以查找每个给定位置的最多供应商 - SQL query to find the most number of vendors per a given location SQL(Left Join) 获取在另一个表中包含所有成分的所有食谱 - SQL(Left Join) Get all recipes which have all their ingredients in another table 使用SQL查找在给定范围内可整除的最小正数 - Find smallest positive number that is Divisible from given range using SQL 配方模型中成分的总和以创建购物清单 - Sum of ingredients in recipes model to create shoppinglist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM