我有一个使用多个UNION ALL的查询。我收到语法错误而无法找到它。我认为这与使用多个工会有关。我只使用MySQL。任何帮助表示赞赏。
错误1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在' ALL附近使用正确的语法(选择a。'在第41行
SELECT Sum(counter) AS counter,
DATE_FORMAT(dtime,'%Y-%m-%d 00:00:00') AS dtime
FROM (
SELECT counter,
dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='980371'
AND counter != 0
UNION ALL
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='979661')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='979661' ))
ORDER BY dtime
AND counter != 0
UNION ALL (this is line 41)
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='984121')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible where dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='984121' ))
ORDER BY dtime
AND counter != 0) AS MainTable
GROUP BY WEEKOFYEAR(dtime)
HAVING SUM(counter) > 0
如果我更改了&#39; id =&#39; id并在其中包含另一个id号并删除第二个union所有它的工作原理。我很确定它与第二个联盟有关。这个语句是动态构建的,所以我不能轻易改为使用in而不是多个union alls。
如果我没有清楚地解释这一点,请告诉我,我会提供更多详细信息。
答案 0 :(得分:1)
检查你所写行之上的陈述(这是第41行) 尝试删除/更新此语句并执行查询。
ORDER BY
dtime
AND counter != 0
我们不能在UNION&amp;和UNION ALL。在所有UNION / UNION ALL语句
之后,排序依据应该是最后一个