我正在使用此查询,但我错了...
SELECT * FROM [Orders]
JOIN [Customers]
ON [Orders].[CustomerID] = [Customers].[CustomerID]
WHERE [Orders].[OrderDate] BETWEEN '2010/1/1' AND '2011/1/1'
AND [Orders].[Total] > 1
我收到CustomerID的重复列名称错误。我不确定如何使用Alias来实现这一点。
有人可以告诉我如何正确地写它。
编辑:
感谢所有的建议,这是我可以用的。
SELECT DISTINCT Orders.CustomerID, Orders.ShipToID, Orders.ShipName, Orders.ShipAddress, Orders.ShipAddress2, Orders.ShipCity, Orders.ShipStateOrProvince, Orders.ShipPostalCode, Orders.Total, Orders.OrderDate, Customers.Profession
FROM Orders
JOIN Customers
ON Orders.CustomerID = Customers.CustomerID
WHERE Orders.OrderDate BETWEEN '4/3/2010' AND '2/20/2011'
AND Orders.Total > 1
再次感谢!
答案 0 :(得分:4)
SELECT [Orders].* FROM [Orders] ...
仅从Orders表中获取列。或者完全跳过*
并明确写出您想要的列。
答案 1 :(得分:3)
您需要在开头指定一个表以避免该错误:
SELECT [Orders].* FROM [Orders]
如果您确实需要两个表中的每一列,则必须单独列出它们,并将重复的CustomerID
列别名。
SELECT
[Orders].CustomerID AS oCustID,
[Customers].CustomerID AS cCustID,
[Orders].anotherColumn,
...
答案 2 :(得分:3)
明确您选择的列,而不是SELECT *
,并且必须从两个表中获取类似命名的列时,请使用别名。
SELECT
Orders.OrderID,
/* alias example, not that you'd need both CustomerID columns */
Orders.CustomerID AS oCustomerID,
Customer.CustomerID AS cCustomerID,
Customers.Name,
Customers.Address,
....
....
FROM [Orders]
JOIN [Customers]
ON [Orders].[CustomerID] = [Customers].[CustomerID]
WHERE [Orders].[OrderDate] BETWEEN '2010/1/1' AND '2011/1/1'
AND [Orders].[Total] > 1
答案 3 :(得分:2)
您希望引用您想要明确的所有列,而不是使用*。
例如
SELECT
C.ID,
O.ID
FROM
[Orders] O
JOIN [Customers] C
ON O.[CustomerID] = C.[CustomerID]
WHERE
O.[OrderDate] BETWEEN '2010/1/1' AND '2011/1/1'
AND O.[Total] > 1