在不同连接上的两个表之间选择相同的列

时间:2017-01-08 12:25:21

标签: sql-server join

我有2张这样的表:

enter image de

有了这个条件

<style type="text/css">
.tab { margin-left: 40px; }
</style>

echo "<strong> Introduction:  </strong>
<p class='tab'>
This is the introduction. This is the introduction. This is the     introduction. This is the introduction. This is the introduction. This is the introduction. This is the introduction. This is the introduction. This is the  introduction. 
</p>";

我想在查询中选择ON dbo.MaterialDescriptions.Id = dbo.Joints.RightMaterialDescriptionId AND dbo.MaterialDescriptions.Id = dbo.Joints.LeftMaterialDescriptionId RightMaterialDescriptionId的商品代码。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

您需要使用不同的别名将表连接两次:

SELECT rmd.ItemCode as RightItemCode, lmd.ItemCode as LeftItemCode [, other columns...]
FROM dbo.Joints j
JOIN dbo.MaterialDescriptions rmd ON j.RightMaterialDescriptionId = rmd.Id
JOIN dbo.MaterialDescriptions lmd ON j.LeftMaterialDescriptionId = lmd.Id

注意:您可能希望使用LEFT JOIN代替INNER JOIN