Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL连接3个表,多个列不包括一个表的结果_Mysql_Join - Fatal编程技术网

MySQL连接3个表,多个列不包括一个表的结果

MySQL连接3个表,多个列不包括一个表的结果,mysql,join,Mysql,Join,我正在尝试在MySQL中连接3个表,需要一些帮助 我的第一张桌子是食谱列表 **recipes** RecipeID | RecipeName 1 | Cheese and Ham Toasty 2 | 20 Minute Pasta 3 | Minute Steak 第二个表是分配给recipes表的配料列表 **ingredients** RecipeID | IngredientID | IngredientName 1 | 1

我正在尝试在MySQL中连接3个表,需要一些帮助

我的第一张桌子是食谱列表

**recipes**
RecipeID | RecipeName
1        | Cheese and Ham Toasty
2        | 20 Minute Pasta
3        | Minute Steak
第二个表是分配给recipes表的配料列表

**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
第三个表是一个表,用户可以使用它添加他们不想看到的配料,目前用户只需要一种配料

**usersList**
IngredientID | userID
1            | 2
我在加入表格时的结果如下:

**recipes**
RecipeID | RecipeName
3        | Minute Steak
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
然而,我的结果要么是我不想要的所有食谱,要么是一个空结果。下面是我正在使用的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

我如何加入这些表,以便获得不包括用户列表中有成分的任何配方的所有配方,并且如果用户没有列出成分,仍然提供结果?提前感谢您的帮助。

使用配方和配料之间的内部联接,配料和用户列表之间的左联接,然后在排除结果中,从用户列表返回的主键不为空

这一输出将包括含有多种成分的食谱的多个条目-可能需要整理

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

您不是在寻找加入。您想查看某些成分不存在的配方。因此,从配方表中选择,并在
WHERE
子句中使用
notexists
notin
限制结果。以下是一个在子句中包含两个
的简单解决方案:

select *
from recipes
where recipeid not in
(
  select recipeid
  from ingredients
  where ingredientid in
  (
    select ingredientid 
    from userslist
    where userid = 2
  )
);

您也可以在此处使用左连接,如下所示:

**recipes**
RecipeID | RecipeName
3        | Minute Steak
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

另请注意:您的数据库没有规范化,这可能会导致将来的问题。应该有一个配料表,每个配料有一条记录(例如ID 1=奶酪),还有一个配方配料表,将一个配方(recipeid)与其配料(IngreditID)链接起来。只是关于您的表结构的一个建议:考虑到您可能有许多相同的配料与多个不同的配方相关,从配料表中删除这种关系(规范化)是明智的。使用交叉引用(联接)表更好地模拟多对多关系:。谢谢,我知道上面提到的成分表有问题。我确实有两个配料表,但对于这个问题,我只是想简化一下。谢谢你的建议,这肯定会帮助别人。谢谢托尔斯滕。我不知道为什么我一直在考虑加入表格。这真的简单多了。真的非常感谢你。