无法找到SQL查询语法错误

时间:2016-08-02 07:14:12

标签: mysql sql syntax

我有一个涉及三个表的查询。这是:

SELECT
   u.email,
   (o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount),
   COUNT( i.id )      
FROM
   orders AS o      
LEFT JOIN
   orders_items AS i 
      ON o.id = i.order_id      
WHERE
   o.status_id =5 
   AND (
      o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount
   ) > 0      
GROUP BY
   user_id      
HAVING
   COUNT( i.id ) > 0      
LEFT JOIN
   users AS u 
      ON o.user_id = u.id      
ORDER BY
   (o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount) DESC
  

错误:#1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以获得正确的语法   使用near' LEFT JOIN用户AS u ON o.user_id = u.id ORDER BY   (o.rec_amount + o.delivery_price' at line 7

如果我删除了第二个LEFT JOIN,当然还有来自该连接表中的列的选择的u.email,它可以工作:

SELECT
   (o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount),
   COUNT( i.id )      
FROM
   orders AS o      
LEFT JOIN
   orders_items AS i 
      ON o.id = i.order_id      
WHERE
   o.status_id =5 
   AND (
      o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount
   ) > 0      
GROUP BY
   user_id      
HAVING
   COUNT( i.id ) > 0      
ORDER BY
   (o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount) DESC

感谢您的帮助。

编辑:谢谢大家,这很有效:

SELECT
   u.email,
   (o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount),
   COUNT( i.id )      
FROM
   orders AS o      
LEFT JOIN
   orders_items AS i 
      ON o.id = i.order_id      
LEFT JOIN
   users AS u 
      ON o.user_id = u.id      
WHERE
   o.status_id =5 
   AND (
      o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount
   ) > 0      
GROUP BY
   u.email      
HAVING
   COUNT( i.id ) > 0      
ORDER BY
   (o.rec_amount+o.delivery_price-o.cheques_amount-o.discount_amount-o.money_amount) DESC

0 个答案:

没有答案