SQL错误,列不存在

时间:2013-01-07 18:19:32

标签: sql join

以下错误:

Unknown column 'Customers.Customer_ID' in 'on clause'

是此代码的结果:

SELECT First_Name, Last_Name, Orders.Order_ID, Orders.Order_Date, Orders.Plant_ID, Orders.Plant_Qty, Orders.Price, Orders.Employee_ID
FROM Customers, Orders
INNER JOIN Orders as t2
ON Customers.Customer_ID = t2.Order_ID;

我无法理解为什么; Customers.Customer_ID是Customers表中的一个字段?

2 个答案:

答案 0 :(得分:2)

可能是因为它含糊不清。您需要删除FROM .. Orders,因为您似乎正在尝试进行显式连接。我还建议为列列表中的所有列名添加前缀。您还应该坚持联接中的别名或表名。

SELECT customers.first_name, 
       customers.last_name, 
       orders.order_id, 
       orders.order_date, 
       orders.plant_id, 
       orders.plant_qty, 
       orders.price, 
       orders.employee_id 
FROM   customers 
       INNER JOIN orders  
               ON customers.customer_id = orders.order_id; 

答案 1 :(得分:0)

我相信Customers -> Orders (一对多关系)并且您正在尝试加入表一对一关系,如果我是正确的我,请更正错误。所以查询应该是这样的。

SELECT First_Name, Last_Name, Orders.Order_ID, Orders.Order_Date, Orders.Plant_ID, Orders.Plant_Qty, Orders.Price, Orders.Employee_ID
FROM Customers
INNER JOIN Orders as t2
ON Customers.Customer_ID = t2.Customer_ID;