Sql server 返回错误结果数的内部联接

Sql server 返回错误结果数的内部联接,sql-server,tsql,Sql Server,Tsql,我的查询返回54行,而它应该返回59行。我觉得我只需要内部连接,因为我不想在结果中包含NULL值。我也尝试过使用左外部联接来过滤掉一些不匹配的值,但它返回的结果太多。我使用的是DISTINCT,所以不应该出现重复的问题 我正在尝试创建的查询: 列出所有成分类别和至少使用一种成分的任何食谱的标题 该类别的成分(如有)。消除所有重复项。2列59行 我的代码: select DISTINCT Ingredients.IngredientClassID, Recipes.RecipeTitle from

我的查询返回54行,而它应该返回59行。我觉得我只需要内部连接,因为我不想在结果中包含NULL值。我也尝试过使用左外部联接来过滤掉一些不匹配的值,但它返回的结果太多。我使用的是DISTINCT,所以不应该出现重复的问题

我正在尝试创建的查询:

列出所有成分类别和至少使用一种成分的任何食谱的标题 该类别的成分(如有)。消除所有重复项。2列59行

我的代码:

select DISTINCT Ingredients.IngredientClassID, Recipes.RecipeTitle
from Ingredients
Inner join Recipe_Ingredients on Ingredients.IngredientID = 
Recipe_Ingredients.IngredientID
Inner join Recipes on Recipe_Ingredients.RecipeID = Recipes.RecipeID;
数据库示意图:
我认为54行是正确的输出。我的查询给出了相同的行数

FROM Ingredient_Classes ic
    LEFT JOIN Ingredients i ON i.IngredientClassID = ic.IngredientClassID
    LEFT JOIN Recipe_Ingredients ri ON ri.ingredientId = i.ingredientId
    JOIN recipes r ON r.recipeid = ri.recipeId
WHERE i.ingredientId IS NOT NULL
GROUP BY ic.IngredientClassDescription, r.recipetitle
ORDER BY recipetitle;

您可以选择distinctrecipe.title并查看输出中是否存在所有配方。所以我不认为我们遗漏了什么。

你为什么期待54排?你的食谱里有54行吗?我建议您添加更多的列,看看您是否确实查询了您所期望的内容。顺便说一句-你的设计有点离谱-配料是一个经典的多对多连接-它应该是ID标识1,1,RecipeId int FK和IngreditID int FK。两者都不应为null。