SQL INNER JOIN多表错误

时间:2015-12-28 20:24:41

标签: mysql sql

SQL:

SELECT * FROM `orderregel` 
INNER JOIN `klant` ON order.KlantId = klant.KlantId 
INNER JOIN `product` ON orderregel.ProductId = product.ProductId 
INNER JOIN `order` ON orderregel.OrderId = order.OrderId;
  

错误:#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以便在'order。,orderregel。,product。,klant。 FROM orderregel INNER附近使用正确的语法。在第1行加入klant O'

我的数据库:订单(OrderId,KlantId(FK)),orderregel(OrderregelId,OrderId(FK),ProductId(FK),KlantId(FK)),klant(KlantId,Naam,Voornaam),产品(ProductId,Naam )

我想在每个有自己订单的orderregel上展示product.Naam,klant.Naam

2 个答案:

答案 0 :(得分:2)

这一行:

INNER JOIN `klant` ON order.KlantId = klant.KlantId 

您还需要勾选MySQL保留字order

INNER JOIN `klant` ON `order`.KlantId = klant.KlantId 

请注意错误的开始位置并告诉您:

  

在'order

附近使用正确的语法

同样的事情:

INNER JOIN `order` ON orderregel.OrderId = order.OrderId;

INNER JOIN `order` ON orderregel.OrderId = `order`.OrderId;

重写:

SELECT * FROM `orderregel` 
INNER JOIN `klant` ON `order`.KlantId = klant.KlantId 
INNER JOIN `product` ON orderregel.ProductId = product.ProductId 
INNER JOIN `order` ON orderregel.OrderId = `order`.OrderId;

编辑:根据OP的评论,但在MySQL order保留字周围有刻度。

SELECT * FROM orderregel 
INNER JOIN klant ON orderregel.KlantId = klant.KlantId 
INNER JOIN product ON orderregel.ProductId = product.ProductId 
INNER JOIN `order` ON orderregel.OrderId = `order`.OrderId;

答案 1 :(得分:0)

快速重新排序内部联接语句可能会有所帮助(未经测试)

SELECT * FROM `orderregel` 
INNER JOIN `klant` ON orderregel.KlantId = klant.KlantId 
INNER JOIN `product` ON orderregel.ProductId = product.ProductId 
INNER JOIN `order` ON order.OrderId = order.OrderId;