原文链接:https://www.cnblogs.com/xuwinwin/p/15877703.html
1、报错写法:
SELECT * FROM t1 ORDER BY month ASC UNION SELECT * FROM t2 ORDER BY month ASC
2、原因与解决办法:
order by不能直接出现在union的子句中,但是可以出现在子句的子句中。
可以通过两个查询分别加括号的方式,改成如下:
(SELECT * FROM t1 ORDER BY month ASC) UNION (SELECT * FROM t2 ORDER BY month ASC)
这种方式的目的是为了让两个结果集先分别order by,然后再对两个结果集进行union。虽然这种方式不报错,但是两个order by并没有效果,这是因为联合查询的结果是整个查询完成后得出的,而不是将子查询挨个完成后拼接的
改成如下:
SELECT * FROM (SELECT * FROM t1 ORDER BY month ASC) t3 UNION SELECT * FROM (SELECT * FROM t2 ORDER BY month ASC) t4
标签:union,month,ASC,报错,子句,order,ORDER,SELECT From: https://www.cnblogs.com/Dongmy/p/18053028