0

我运行以下查询,我得到了模棱两可的错误。为什么会这样?

select * 
from dbo.tableA as table1, dbo.tableZ as table2
where (columnB = dbo.tableZ.columnB)

不明确的列名 columnB

tableA 还有一个名为 columnB 的列。

4

2 回答 2

3

两个表都有一个columnB,但你的条件的第一部分没有指定使用哪个表columnB

将其更改为:

select * 
from dbo.tableA as table1, dbo.tableZ as table2
where (table1.columnB = table2.columnB)
于 2013-04-10T08:27:09.523 回答
2

您需要为字段名称使用别名。在创建连接时使用别名引用您的字段是一个很好的规则,以便其他人可以更轻松地阅读 SQL...

例如

SELECT  t1.ColA ,
        t1.ColB ,
        t2.ColA
FROM    Table1 AS t1
        INNER JOIN Table2 AS t2 ON t1.Id = t2.FId

代替...

SELECT  ColA ,
        ColB ,
        ColA
FROM    Table1 AS t1
        INNER JOIN Table2 AS t2 ON t1.Id = t2.FId

我知道第二个 SQL 查询不会解析,但这只是一个示例。

于 2013-04-10T08:34:41.037 回答