SQL查询语法错误,其中语法看起来是正确的

时间:2013-04-08 15:07:18

标签: mysql

我不知道这个查询有什么问题。对我来说似乎很好。

select o.id, p.date, u.billingName, u.billingPhone, u.billingCompany,
       u.billingAddress, u.billingCity, u.billingState, u.billingCountry,
       u.billingZip, u.shippingName, u.shippingPhone, u.shippingCompany,
       u.shippingAddress, u.shippingCity, u.shippingState, u.shippingCountry,
       u.shippingZip, o.layer, o.boardSizeWidth,o.boardSizeHeight, o.quantity
from order o, purchase p, useraccount u
where p.id = 1 and o.id = p.OrderId and u.id = p.UserId 
  

错误:#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'order o,buy p,useraccount u附近使用正确的语法,其中p.id = 1,o.id = p.OrderId和u.id'在第1行

2 个答案:

答案 0 :(得分:4)

orderreserved word,需要使用反引号进行转义。

在加入表时,也应该使用标准的ANSI JOIN语法。请参阅下文,了解如何做到这一点。

select ....
from `order` o
inner join purchase p
    on o.id = p.OrderId
inner join useraccount u 
    on u.id = p.UserId
where p.id = 1

作为旁注,您应该避免对表名和列名使用保留字。

答案 1 :(得分:0)

ORDER是保留关键字。必须使用反引号来逃避。

....FROM `Order` o

如果您有机会更改标识符,请更改保留关键字列表中不存在的表名。这将避免您将来的头痛。

相关问题