我们有两个表,一个带有ID,另一个带有名称。有些id不是必需的(可以为空),例如:
rowId item1Id, item2Id, item3Id (item3Id can be null)
在我们加入这两个表后,我们得到了id和名字。但是,如果item3 Id为null,则不显示该行。
一些数据:
表1
rowId,item1Id,item2Id,item3Id
1,2,3,4
2,1,5,NULL
在我们的例子中,我们得到一行(1行)
因此,如果它在表2中没有存在,并且没有显示该行,但我们希望显示null。 所以当我们离开连接这两个表时,结果应该有2行。
这是sql:
SELECT Recipes.recipeId, Recipes.userId,Users.firstName + ' ' + Users.lastName as userName, servingTypeId,
Codings.coding as servingType, categoryId, Codings_2.coding as healthAspects, continentId,
Codings_3.coding as continent, countryId, CodingsAssociated.coding as country, typeOfPreparationId,
Codings_4.coding as typeOfPreparation, flavourId, Codings_5.coding as flavour, preparationSkillId,
Codings_6.coding as preparationSkill, seasonId, activePreparationTime,
overallPreparationTime, isLocalDelight, servings, Codings_7.coding as season ,
calories, youTubeId, datePosted, isComposite, Recipes.isApproved, Recipes.timestamp, title,
localDelightRegion, otherFeatures, cookingInstructions
FROM Recipes LEFT OUTER JOIN
Codings ON Recipes.servingTypeId = Codings.codingKeyId LEFT OUTER JOIN
Codings as Codings_2 ON Recipes.categoryId = Codings_2.codingKeyId LEFT OUTER JOIN
Codings as Codings_3 ON Recipes.continentId = Codings_3.codingKeyId LEFT OUTER JOIN
CodingsAssociated ON Recipes.countryId = CodingsAssociated.codingKeyId LEFT OUTER JOIN
Codings as Codings_4 ON Recipes.typeOfPreparationId = Codings_4.codingKeyId LEFT OUTER JOIN
Codings as Codings_5 ON Recipes.flavourId = Codings_5.codingKeyId LEFT OUTER JOIN
Codings as Codings_6 ON Recipes.preparationSkillId = Codings_6.codingKeyId LEFT OUTER JOIN
Codings as Codings_7 ON Recipes.seasonId = Codings_7.codingKeyId LEFT OUTER JOIN
RecipesTranslations ON Recipes.recipeId = RecipesTranslations.recipeId LEFT OUTER JOIN
Users ON Recipes.userId = Users.userId
WHERE CodingsAssociated.languageId = @languageId AND Codings.languageId = @languageId
AND Codings_2.languageId = @languageId AND Codings_3.languageId = @languageId
AND Codings_4.languageId = @languageId AND
Codings_5.languageId = @languageId AND Codings_6.languageId = @languageId AND RecipesTranslations.languageId = @languageId
AND Codings_7.languageId = @languageId
例如因为seasonId有时为null,所以Codings_7.coding的连接没有显示,但是我也不会显示这些行,其中id为null,名称也为null。
答案 0 :(得分:2)
很难说,这里要问的是什么,但我认为你需要做一个外连接,并且从tsql标签我想你使用MS SQL服务器,所以在这里你去:http://msdn.microsoft.com/en-us/library/aa213228(v=sql.80).aspx < / p>
e.g:
SELECT *
FROM table1 a LEFT OUTER JOIN table2 b
ON a.id_to_join_on = b.id_to_join_on
答案 1 :(得分:2)
正如Nikola Markovinović所述,您需要将where条件移动到连接条件
这里你的选择更正了:
SELECT
Recipes.recipeId,
Recipes.userId,
---
cookingInstructions
FROM
Recipes
LEFT OUTER JOIN Codings ON
Recipes.servingTypeId = Codings.codingKeyId AND
Codings.languageId = @languageId
LEFT OUTER JOIN Codings as Codings_2 ON
Recipes.categoryId = Codings_2.codingKeyId AND
Codings_2.languageId = @languageId
LEFT OUTER JOIN Codings as Codings_3 ON
Recipes.continentId = Codings_3.codingKeyId AND
Codings_3.languageId = @languageId
LEFT OUTER JOIN CodingsAssociated ON
Recipes.countryId = CodingsAssociated.codingKeyId AND
CodingsAssociated.languageId = @languageId
LEFT OUTER JOIN Codings as Codings_4 ON
Recipes.typeOfPreparationId = Codings_4.codingKeyId AND
Codings_4.languageId = @languageId
LEFT OUTER JOIN Codings as Codings_5 ON
Recipes.flavourId = Codings_5.codingKeyId AND
Codings_5.languageId = @languageId
LEFT OUTER JOIN Codings as Codings_6 ON
Recipes.preparationSkillId = Codings_6.codingKeyId AND
Codings_6.languageId = @languageId
LEFT OUTER JOIN Codings as Codings_7 ON
Recipes.seasonId = Codings_7.codingKeyId AND
Codings_7.languageId = @languageId
LEFT OUTER JOIN RecipesTranslations ON
Recipes.recipeId = RecipesTranslations.recipeId AND
RecipesTranslations.languageId = @languageId
LEFT OUTER JOIN Users ON
Recipes.userId = Users.userId ;