SQL server ERROR不明确的列名

时间:2016-08-23 16:40:18

标签: sql-server sql-server-2008 inner-join

无法理解此SQL查询中的错误:

USE [My_db]
GO

SELECT ItemId
      ,Subject
      ,CreatedOn
  FROM ItemBase AS e
      INNER JOIN ItemExtensionBase AS p
      ON e.ItemId = p.ItemId
GO

错误:

 Msg 209, Level 16, State 1, Line 4
 Ambiguous column name 'ItemId'.
两个表中都存在

ItemId列。

2 个答案:

答案 0 :(得分:4)

USE [My_db]
GO

SELECT e.ItemId
      ,Subject
      ,CreatedOn
  FROM ItemBase AS e
      INNER JOIN ItemExtensionBase AS p
      ON e.ItemId = p.ItemId
GO

您需要告诉它从

中取出itemid字段的表

答案 1 :(得分:3)

使用e.ItemIdp.ItemId

USE [My_db]
GO

SELECT e.ItemId
      ,Subject
      ,CreatedOn
  FROM ItemBase AS e
      INNER JOIN ItemExtensionBase AS p
      ON e.ItemId = p.ItemId
GO