1

以下错误:

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 是客户表中的一个字段吗?

4

2 回答 2

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; 
于 2013-01-07T18:21:12.890 回答
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;
于 2013-01-07T18:23:36.963 回答