[英]MySQL join 3 tables, with multiple columns excluding results from one table
I'm trying to join 3 tables in MySQL, and need some help. 我正在尝试在MySQL中加入3个表,并且需要一些帮助。
My first table is a list of recipes. 我的第一张桌子是食谱列表。
**recipes**
RecipeID | RecipeName
1 | Cheese and Ham Toasty
2 | 20 Minute Pasta
3 | Minute Steak
The second table is a list of ingredients assign to the recipes table 第二张表是分配给配方表的配料列表
**ingredients**
RecipeID | IngredientID | IngredientName
1 | 1 | Cheese
1 | 2 | Bread
1 | 3 | Ham
1 | 4 | Butter
2 | 5 | Pasta
2 | 6 | Mince
2 | 1 | Cheese
3 | 8 | Steak
3 | 9 | BBQ Sauce
The third table is a table that users can use to add ingredients they don't want to see recipes of, for now only one ingredient for the user 第三个表是用户可以用来添加他们不想看到其配方的配料的表,目前用户仅可以使用一种配料
**usersList**
IngredientID | userID
1 | 2
And my result when joining the tables should be as follows: 和我加入表时的结果应该如下:
**recipes**
RecipeID | RecipeName
3 | Minute Steak
However my result is either all the recipes I don't want or an empty result. 但是我的结果要么是我不想要的所有食谱,要么是空的结果。 Below is the MySQL that I'm using that currently supplies me with all the recipes I don't want.
下面是我正在使用的MySQL,该MySQL当前为我提供了所有我不需要的食谱。
SELECT RecipeID, RecipeName FROM recipes LEFT JOIN ingredients
INNER JOIN usersList ON ingredients.IngredientID = usersList.IngredientID
ON recipes.RecipeID = ingredients.RecipeID
WHERE recipes.RecipeID IS NOT NULL
AND usersList.userID = 2
GROUP BY recipes.RecipeID
How can I join these tables so that I get all recipes that don't include any of the recipes that have ingredients in the user list and still supplies results if the user has no ingredients listed? 如何加入这些表格,以便获得所有不包含用户列表中包含成分的任何食谱的食谱,并且如果用户未列出成分,仍然可以提供结果? Thanks in advance for helping.
在此先感谢您的帮助。
You are not looking for a join. 您不是要加入。 You want to see recipes for which not exists a certain ingrediant.
您想查看不存在某种配方的配方。 So select from the recipes table and limit the results with
NOT EXISTS
or NOT IN
in the WHERE
clause. 因此,从配方表中进行选择,并在
WHERE
子句中使用NOT EXISTS
或NOT IN
限制结果。 Here is a simple solution with two IN
clauses: 这是带有两个
IN
子句的简单解决方案:
select *
from recipes
where recipeid not in
(
select recipeid
from ingredients
where ingredientid in
(
select ingredientid
from userslist
where userid = 2
)
);
Use an inner join between recipes and ingredients, a left join between ingredients and userlists, then in the exclude results where the Primary key returned from the userlist is not null. 在配方和配料之间使用内部联接,在配料和用户列表之间使用左联接,然后在排除结果中用户列表返回的主键不为空。
The output of this will include multiple entries for recipes with multiple ingredients - which may need tidying.... 此输出将包含具有多种成分的配方的多个条目-可能需要整理...。
SELECT recipename, GROUP_CONCAT(ingredient_name),
SUM(IF(userlist.ingredientid IS NULL, 0, 1))
FROM recipes
INNER JOIN ingredients
ON recipes.recipeid=ingredients.ingredientid
LEFT JOIN userlist
ON ingredients.ingredientid=userlist.ingredientid
AND userlist.userid=_______
GROUP BY recipename
HAVING SUM(IF(userlist.ingredientid IS NULL, 0, 1))=0
You can also use a left join here as follows: 您还可以在此处使用左联接,如下所示:
select r.RecipeID, r.RecipeName from recipes r
left join (select RecipeID from ingredients i
join usersList u on u.RecipeID = i.RecipeID) as u
on u.RecipeID = r.RecipeID
where u.RecipeID is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.