不是唯一的表/别名 - SQL

时间:2016-10-26 00:32:47

标签: mysql sql

当我运行join语句时,我收到错误“Not a unique table / alias'Customer'”。我想显示我拥有的所有信息。我研究过JOIN语句,但我找不到什么错误。

    SELECT Customer.CustomerID, Customer.FirstName, Customer.LastName,     Customer.StreetAddress,Customer.City,Customer.State,Customer.Zipcode, Customer.HomePhone,Customer.MobilePhone,Customer.OtherPhone, Pizza.PizzaID,
Pizza.PizzaName, Pizza.Description, Pizza.UnitPrice, OrderInformation.OrderID, OrderInformation.OrderDate, OrderItem.Quantity  


 FROM Customer

 JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
 JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
 JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;

2 个答案:

答案 0 :(得分:0)

SELECT
...
FROM Customer
...
JOIN Customer

您选择FROM Customer,然后选择JOIN Customer,这意味着您现在有两个Customer个实例。当您引用Customer.CustomerID之类的内容时,您的查询并不知道您所引用的Customer表的哪个迭代。

如果您确实需要对同一个表的两个引用,则可以给一个别名。但是,由于您引用了OrderItem表,但从未JOIN或选择FROM,我预感到其中一个Customer表应为{{1} }。也许是这样......

OrderItem

答案 1 :(得分:0)

我强烈建议您使用表别名。看来Customer的第一个引用应该是OrderItem

SELECT c.CustomerID, c.FirstName, c.LastName,       
       c.StreetAddress, c.City, c.State, c.Zipcode, 
       c.HomePhone, c.MobilePhone, c.OtherPhone,
       p.PizzaID, p.PizzaName, p.Description, p.UnitPrice, 
       oinf.OrderID, oinf.OrderDate, oi.Quantity  
FROM OrderItem oi JOIN
     OrderInformation oinf
     ON oinf.OrderID = oi.OrderID JOIN
     Pizza p
     ON p.PizzaID = oi.PizzaID JOIN
     Customer c
     ON c.CustomerID = oi.CustomerID;