我正在编写SQL查询并在执行查询时收到错误。
SELECT tableA.*,
A.tableB.Id AS Item,
B.tableB.Id AS Number
FROM tableA
JOIN tableB AS A
ON tableA.DefinitionId1 = tableB.DefinitionId
JOIN tableB AS B
ON tableA.DefinitionId2 = tableB.DefinitionId
这给了我错误:
Msg 4104, Level 16, State 1, Line 6
The multi-part identifier "tableB.DefinitionId" could not be bound.
Msg 4104, Level 16, State 1, Line 8
The multi-part identifier "tableB.DefinitionId" could not be bound.
Msg 207, Level 16, State 1, Line 2
Invalid column name tableB'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'tableB'.
我正在尝试从tableA中获取2列,并将它们与tableB中的1列匹配。为什么它会给我这个错误,即使我正在为这两个选项创建别名,以及我有什么选择来解决这个问题?
答案 0 :(得分:3)
将您的查询更改为:
SELECT
A.*,
B1.Id AS Item,
B2.Id AS Number
FROM tableA A
JOIN tableB AS B1
ON A.DefinitionId1 = B1.DefinitionId
JOIN tableB AS B2
ON A.DefinitionId2 = B2.DefinitionId
如果您已为表定义了别名,请继续在整个查询中使用它(加入和SELECT
部分)