如何对具有相同名称的列执行 INNER JOIN

How to perform an INNER JOIN on columns with the same name

我实际上是将用户 table 中列 "LocationId" 的内容与 table ChaplainLocation 中列 "LocationId" 的内容进行交换。

我尝试了一些 INNER JOIN 语句,但在区分两列时遇到了 运行 问题。我也试过设置别名也没用。

SELECT a.LocationID FROM [User] AS table1a,
          a.ChaplainId,
          a.FullName,
          b.LocationId FROM [ChaplainLocation] AS table2b,
          b.ChaplainId
   FROM   table1 a
          INNER JOIN table2 b
             ON a.LocationId = b.LocationId'''

我知道上面的 SQL 很乱,但我是 INNER JOIN 的新手。我需要来自 table ChaplainLocation 的 LocationId 来替换用户 table.

中的 LocationId

您当前的语法略有偏差。 Table 别名位于 table 名称旁边,出现在 FROMJOIN 子句中。

SELECT
    a.LocationID AS LocationID_a,
    a.ChaplainId AS ChaplainId_a,
    a.FullName,
    b.LocationId AS LocationId_b,
    b.ChaplainId AS ChaplainId_b
FROM [User] AS a
INNER JOIN [ChaplainLocation] AS b
    ON a.LocationId = b.LocationId;

请注意,引用多个具有相同名称的列并没有错,只要您使用 table 别名(或完整的 table 名称)限定该列引用即可区分一下。