简体   繁体   English

SQL-多对多查询

[英]SQL — many-to-many query

I'm trying to select all recipes which contain a given number of ingredients . 我正在尝试选择所有包含给定数量成分的食谱 I'm able to find all recipes based on one given recipe_id with: 我可以使用以下给定的cipal_id查找所有食谱:

SELECT name
FROM recipe
INNER JOIN recipe_ingredient
ON recipe.recipe_id = recipe_ingredient.recipe_id
WHERE recipe_ingredient.recipe_id = ?

But I'm having trouble figuring out what the query should look like when I'm looking for recipes which contain more than contain more than one specific ingredient . 但是我在寻找包含多种成分的食谱时,很难弄清楚查询的样子。 For Example Ingredient A and Ingredient B. 例如成分A和成分B。

My tables look like this: 我的表如下所示:

ingredient
  -ingredient_id
  -name

recipe_ingredient
  -recipe_ingredient
  -ingredient_id
  -recipe_id


recipe
  -recipe_id
  -name

I would be very happy about any ideas on how to solve that problem! 对于解决该问题的任何想法,我将感到非常高兴! Thanks. 谢谢。

Your query will look something like this 您的查询将如下所示

Your query will look something like this

SELECT name, count(*)
FROM recipe
INNER JOIN recipe_ingredient
ON recipe.recipe_id = recipe_ingredient.recipe_id
GROUP BY name 
HAVING count(*) > 1

In order to match all elements of an IN clause, you need to make sure you select only Recipes that have a count which matches the total number of Ingredients in your list: 为了匹配IN子句的所有元素,您需要确保仅选择count与列表中成分总数匹配的配方:

SELECT name
FROM recipe
INNER JOIN recipe_ingredient
ON recipe.recipe_id = recipe_ingredient.recipe_id
WHERE recipe_ingredient.ingredient_id IN (ID1, ID2, ID3) --list of ingredient IDs
Group By Name
Having Count(*) = 3 --# of Ingredients you have chosen

Good luck finding which recipe will work with the ingredients you have available 祝您好运,找到适合您的食材的食谱

Here is a functional example 这是一个功能示例

IF looking for specific Ingredients, you could do a pre-query doing a union of all the ingredients you are interested in. Join that to the ingredients table per recipe and make sure that all ingredients are accounted for. 如果要查找特定的成分,则可以进行预查询,对所有感兴趣的成分进行合并。将其加入每个配方的成分表中,并确保已考虑所有成分。 This is handled by the group by and having count = the number of ingredients you are looking for. 这是由小组来处理的,计数为=您要寻找的配料数量。

I did this example based on the name of the ingredient. 我根据成分名称做了此示例。 If you have/know the actual ingredient ID (which would be more accurate such as web-based and you have the IDs chosen by a user), just change the join condition to the ingredient ID instead of just the description of the ingredient. 如果您具有/知道实际的原料ID(可能更准确,例如基于Web,并且您有用户选择的ID),则只需将加入条件更改为原料ID,而不仅仅是原料的描述。

SELECT
      r.name,
      r.recipe_id
   from
      ( SELECT 'Milk' as Ingredient
           UNION select 'Eggs'
           UNION select 'Butter' ) as Wanted
         JOIN recipe_ingredient ri
            ON Wanted.Ingredient = ri.recipe_ingredient
            JOIN Recipe r
               ON ri.Recipe_id = r.id
   group by
      ri.recipe_id
   having
      COUNT(*) = 3

In this case, Milk, Butter, Eggs and a final count = 3. 在这种情况下,牛奶,黄油,鸡蛋和最终数量= 3。

Use group by and having clause 使用分组依据和具有从句

SELECT name
FROM recipe
INNER JOIN recipe_ingredient
ON recipe.recipe_id = recipe_ingredient.recipe_id
GROUP BY name
HAVING count(1) > 1

Just use OR for your where . 只需将OR用于您的where Like this 像这样

$query = "SELECT name
FROM recipe
INNER JOIN recipe_ingredient
ON recipe.recipe_id = recipe_ingredient.recipe_id
WHERE recipe_ingredient.recipe_id = ? OR recipe_ingredient.recipe_id = ?";

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

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