高级查询代码1064 MySQL出错

时间:2018-05-21 20:29:36

标签: mysql sql

当我尝试执行以下查询时,我一直收到错误:

SELECT C.CustomerID WITH C.CustomerName WITH O.OrderDate WITH O.TotalOrderAmount
FROM CUSTOMERS C
LEFT JOIN [ORDERS] 0
ON C.CustomerID=O.CustomerID

UNION

SELECT C.CustomerID, C.CustomerName, O.OrderDate, O.TotalOrderAmount
FROM CUSTOMERS C
RIGHT JOIN [ORDERS] 0
On C.CustomerID-O.CustomerID;

以下是我在MySQL中收到的代码:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[ORDERS] 0 ON C.CustomerID=O.CustomerID  UNION  SELECT C.CustomerID, C.CustomerN' at line 3

我们非常感谢您提供的纠正此错误的任何帮助。

2 个答案:

答案 0 :(得分:0)

表别名不能是数字。 withas不同。试试这个版本:

SELECT C.CustomerID, C.CustomerName, O.OrderDate, O.TotalOrderAmount
FROM CUSTOMERS C LEFT JOIN
     [ORDERS] o  -- not 0
     ON C.CustomerID = O.CustomerID
UNION
SELECT C.CustomerID, C.CustomerName, O.OrderDate, O.TotalOrderAmount
FROM CUSTOMERS C RIGHT JOIN
     [ORDERS] o  -- not 0
     On C.CustomerID = O.CustomerID;  -- not -

我还应该提一下,这不是(在我看来)制定full join的最佳方式。

答案 1 :(得分:0)

只需完全加入而不是联盟。应该在这里给你相同的结果:

SELECT C.CustomerID, C.CustomerName, O.OrderDate, O.TotalOrderAmount
FROM CUSTOMERS C
full JOIN [ORDERS] 0
On C.CustomerID-O.CustomerID