1

我在 SQL Server 中为 Select 做了一个声明

SELECT Oid      AS 'Order ID',
       Eid      AS 'Employee ID',
       Barcode,
       PacksQty AS 'Pack Quantity',
       UnitQty  AS 'Units Quantity',
       Date     AS 'Order Date',
       Price    AS 'Total Price'
FROM   OrderDetails
       INNER JOIN Orders
         ON OrderDetails.Oid = Orders.Oid 

并且选择Oid的错误和错误是“Ambiguous column name 'Oid'”

4

1 回答 1

0

Oid是模棱两可的,因为它在两个表中。与自然联接(未在 SQL Server 中实现)不同,您需要指定要从中SELECT获取值的表。

这似乎是多余的,因为您的查询已经指定OrderDetails.Oid = Orders.Oid,但两个值实际上可以不同(例如,如果OrderDetails.Oid'abc'Orders.Oid'ABC',并且在不区分大小写的排序规则上)。

您可以定义表别名以避免输入整个表名。下面的示例语法(对您的架构做出一些假设)

SELECT O.Oid      AS 'Order ID',
       O.Eid      AS 'Employee ID',
       OD.Barcode,
       OD.PacksQty AS 'Pack Quantity',
       OD.UnitQty  AS 'Units Quantity',
       OD.Date     AS 'Order Date',
       OD.Price    AS 'Total Price'
FROM   OrderDetails OD
       INNER JOIN Orders O
         ON OD.Oid = O.Oid 
于 2013-07-03T23:15:55.280 回答