MySql语法错误(使用连接)

时间:2017-07-22 02:01:10

标签: c# mysql mysql-error-1064

我在MYSQL语法中找不到错误:

"SELECT customer.CustomerCode, customer.CustomerName, customer.CustomerAddress, customer.CustomerHandphone, customer,CustomerEmail, product.ProductCode, product.ProductName, product.ProductPrice, orderdetail.OrderCode, orderdetail.ProductCode, orderdetail.ProductPrice, orderlist.OrderCode, orderlist.CustomerCode, orderlist.OrderPrice " +
                     "FROM customer INNER JOIN (product INNER JOIN (orderlist INNER JOIN(orderdetail INNER JOIN ON orderlist.OrderCode = orderdetail.OrderCode) ON product.ProductCode = orderdetail.ProductCode) ON customer.CustomerCode = orderlist.CustomerCode) " +
                     "Where orderlist.OrderCode =@orderdetail.OrderCode " +
                        "GROUP BY customer.CustomerCode, customer.CustomerName, customer.CustomerAddress, customer.CustomerHandphone, customer,CustomerEmail, product.ProductCode, product.ProductName, product.ProductPrice, orderdetail.OrderCode, orderdetail.ProductCode, orderdetail.ProductPrice, orderlist.OrderCode, orderlist.CustomerCode, orderlist.OrderPrice";"

错误如下 1064 - 您的SQL语法出错;检查与MariaDB服务器版本对应的手册,以便在'ON orderlist.OrderCode = orderdetail.OrderCode'附近使用正确的语法ON产品.ProductCode = orderde'在第1行

1 个答案:

答案 0 :(得分:1)

在每个ON之后用JOIN子句编写查询。这是更容易阅读和写,可能是你的问题:

SELECT c.CustomerCode, c.CustomerName, c.CustomerAddress, c.CustomerHandphone, c.CustomerEmail,
       p.ProductCode, p.ProductName, p.ProductPrice,
       od.OrderCode, od.ProductCode, od.ProductPrice, od.OrderCode,
       ol.CustomerCode, ol.OrderPrice " +
FROM orderlist ol INNER JOIN
     orderdetail od
     ON ol.orderCode = od.OrderCode INNER JOIN
     customer c 
     ON c.CustomerCode = ol.CustomerCode INNER JOIN
     product p
     ON p.ProductCode = od.ProductCode;

此外,ORDER BY似乎没必要。通常,它将与聚合函数一起使用。如果你以某种方式获得重复(似乎不太可能),请使用SELECT DISTINCT

最后,我不明白这一点:

Where ol.OrderCode = @orderdetail.OrderCode

我认为@orderdetail.OrderCode不是有效的语法。如果你想传入一个变量,它看起来更像是:

Where ol.OrderCode = @OrderCode