SQL在多个JOIN上保持Join Error

时间:2016-06-27 08:18:16

标签: mysql

Here is my query 

SELECT item.item, item.id as itemID,item.item_sku,O.serial_no,transaction.id, transaction.t_price, transaction.t_unit, transaction.total_amount, transaction.transaction_type, transaction.comment, transaction.created from transaction

LEFT JOIN item ON transaction.item_id = item.id
LEFT JOIN order O  ON transaction.order_no=o.order_no
WHERE transaction_type='buy' ORDER BY transaction.created DESC

但是它显示了这个错误..为什么??

1064 - 您的SQL语法出错;检查与您的MariaDB服务器版本对应的手册,以便在正确的语法附近使用o订单o ON transaction.order_no = o.order_no WHERE transaction_type =' buy'订单'在行

2 个答案:

答案 0 :(得分:2)

ORDERreserved word,所以你需要逃避它。检查此Stackoverflow answer,了解如何使用反向勾号来转义查询中的保留字。

请检查一下:

SELECT item.item, item.id as itemID,item.item_sku,O.serial_no,transaction.id, transaction.t_price, transaction.t_unit, transaction.total_amount, transaction.transaction_type, transaction.comment, transaction.created 
FROM transaction

LEFT JOIN item ON transaction.item_id = item.id
LEFT JOIN `order` O  ON transaction.order_no = o.order_no
WHERE transaction_type='buy' 
ORDER BY transaction.created DESC

答案 1 :(得分:0)

使用ALIAS时可以写一个较短的表格。

SELECT
    i.item
    , i.id as itemID
    , i.item_sku
    , o.serial_no
    , t.id
    , t.t_price
    , t.t_unit
    , t.total_amount
    , t.transaction_type
    , t.comment
    , t.created
FROM transaction
LEFT JOIN item i ON t.item_id = i.id
LEFT JOIN `order` o ON t.order_no=o.order_no
WHERE transaction_type='buy'
ORDER BY t.created DESC;