SQL Server:内部联接:模糊名称

时间:2018-01-05 11:39:42

标签: sql sql-server tsql inner-join

我在SQL Server中有两个表:

[制造] [DBO] [订单明细]:

CaseId    Gender
-----------------
698       Female
694       Male
676       Male
659       Male
636       Female
624       Female
622       Female
597       Male
579       Male
574       Male

[制造]。[DBO]。[订购]

OrderID   SoftAdminOrderID    InfoIDOrderID   Active      
----------------------------------------------------
33425999      698             196665           1
33425984      694             196662           1
33425984      694             196663           0   
33425609      676             196645           1
33425270      659             196625           1
33424973      636             196609           1
33424716      624             196594           1
33424704      622             196592           1
33424500      597             196568           1
33424340      579             196552           1
33424281      574             196548           1

我希望通过内部联接获得性别:

 SELECT TOP (10) 
     [OrderID],
     [SoftAdminOrderID],
     [InfoID],
     [Active],
    /* [Gender]*/
 FROM
     [Production].[dbo].[Order]
 /*INNER JOIN 
     [Production].[dbo].[OrderDetails] ON [Production].[dbo].[Order].SoftAdminOrderID = [Production].[dbo].[OrderDetails].CaseId*/
 where Active = 1
 order by SoftAdminOrderID desc

但是当我从内连接中删除注释时出现错误:

  

Msg 209,Level 16,State 1,Line 58
  不明确的列名'CaseId'。

我不明白为什么它被认为是含糊不清的。

1 个答案:

答案 0 :(得分:1)

如果真的发生了,那么为什么不通过在列中添加表别名来解决它:

SELECT TOP (10) 
    o.[OrderID]
    ,o.[SoftAdminOrderID]
    ,o.[InfoID]
    ,od.[Gender]
 FROM [Production].[dbo].[Order] AS o
 INNER JOIN [Production].[dbo].[OrderDetails] AS od ON o.SoftAdminOrderID = od.CaseId