MySQL错误#1064 - SQL语法错误

时间:2014-08-26 19:27:52

标签: mysql sql mariadb

我在Maria Db

上为MySQL构建了以下查询,版本5.5.37
SELECT (
   Coalesce(`w`.`ID`, "") AS `w_ID`,
   Coalesce(`w`.`reportID`, "") AS `w_reportID`,
   Coalesce(`w`.`date`, "") AS `w_date`,
   Coalesce(`w`.`amount`, "") AS `w_amount`,
   Coalesce(`w`.`add25`, "") AS `w_add25`,
   Coalesce(`w`.add50, "") AS `w_add50`,
   Coalesce(`w.deplacement`, "") AS `w_deplacement`,
   Coalesce(`t.ID`, "") AS `t_id`,
   Coalesce(`t.reportID`, "") AS `t_reportID`,
   Coalesce(`t.date`, "") AS `t_date`,
   Coalesce(`t.time`, "") AS `t_time`,
   Coalesce(`t.amount`, "") AS `t_amount`,
   Coalesce(`t.unit`, "") AS `t_unit`,
   Coalesce(`t.price`, "") AS `t_price`;
FROM workhours w
LEFT JOIN trip t on w.reportID = t.reportID 
WHERE t.reportID = 13)
UNION
SELECT (
   Coalesce(`w1`.`ID`, "") AS `w_ID`,
   Coalesce(`w1`.`reportID`, "") AS `w_reportID`, 
   Coalesce(`w1`.`date`, "") AS `w_date`,
   Coalesce(`w1`.`amount`, "") AS `w_amount`,
   Coalesce(`w1`.`add25`, "") AS `w_add25`,
   Coalesce(`w1`.add50, "") AS `w_add50`,
   Coalesce(`w1.deplacement`, "") AS `w_deplacement`,
   Coalesce(`t1.ID`, "") AS `t_id`,
   Coalesce(`t1.reportID`, "") AS `t_reportID`,
   Coalesce(`t1.date`, "") AS `t_date`,
   Coalesce(`t1.time`, "") AS `t_time`,
   Coalesce(`t1.amount`, "") AS `t_amount`,
   Coalesce(`t1.unit`, "") AS `t_unit`,
   Coalesce(`t1.price`, "") AS `t_price`
FROM `workhours` w1
RIGHT JOIN `trip` t1 on `w1`.`reportID` = `t1`.`reportID` 
WHERE `t1`.`reportID` = 13)

当我运行它时,我收到以下错误代码:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `w_ID`, Coalesce(`w`.`reportID`, "") AS `w_reportID`, Coalesce(`w`.`da' at line 2 

我不知道这条线路应该出现什么问题? 是否有人有想法来解决这个问题

我希望我提供足够的信息。

2 个答案:

答案 0 :(得分:2)

select (的语法不正确,第一次查询中有;

SELECT
   Coalesce(`w`.`ID`, "") AS `w_ID`,
   Coalesce(`w`.`reportID`, "") AS `w_reportID`,
   Coalesce(`w`.`date`, "") AS `w_date`,
   Coalesce(`w`.`amount`, "") AS `w_amount`,
   Coalesce(`w`.`add25`, "") AS `w_add25`,
   Coalesce(`w`.add50, "") AS `w_add50`,
   Coalesce(`w.deplacement`, "") AS `w_deplacement`,
   Coalesce(`t.ID`, "") AS `t_id`,
   Coalesce(`t.reportID`, "") AS `t_reportID`,
   Coalesce(`t.date`, "") AS `t_date`,
   Coalesce(`t.time`, "") AS `t_time`,
   Coalesce(`t.amount`, "") AS `t_amount`,
   Coalesce(`t.unit`, "") AS `t_unit`,
   Coalesce(`t.price`, "") AS `t_price`
FROM workhours w
LEFT JOIN trip t on w.reportID = t.reportID AND w.date = t.date
WHERE t.reportID = 13
UNION
SELECT
   Coalesce(`w1`.`ID`, "") AS `w_ID`,
   Coalesce(`w1`.`reportID`, "") AS `w_reportID`, 
   Coalesce(`w1`.`date`, "") AS `w_date`,
   Coalesce(`w1`.`amount`, "") AS `w_amount`,
   Coalesce(`w1`.`add25`, "") AS `w_add25`,
   Coalesce(`w1`.add50, "") AS `w_add50`,
   Coalesce(`w1.deplacement`, "") AS `w_deplacement`,
   Coalesce(`t1.ID`, "") AS `t_id`,
   Coalesce(`t1.reportID`, "") AS `t_reportID`,
   Coalesce(`t1.date`, "") AS `t_date`,
   Coalesce(`t1.time`, "") AS `t_time`,
   Coalesce(`t1.amount`, "") AS `t_amount`,
   Coalesce(`t1.unit`, "") AS `t_unit`,
   Coalesce(`t1.price`, "") AS `t_price`
FROM `workhours` w1
RIGHT JOIN `trip` t1 on `w1`.`reportID` = `t1`.`reportID` AND w1.date = t1.date
WHERE `t1`.`reportID` = 13

这样,您可以在括号

之后向整个结果集添加订单

答案 1 :(得分:0)

不要像你拥有的那样把()放在选择内。你不需要在where子句之后(在select或之后)。将它们都移除。如果由于某种原因需要它们(在select关键字之前。