mysql Incorrect usage of UNION and ORDER BY 错误备忘

时间:2024-08-08 08:33:50

出现这个错误的语句是酱紫的

select xxx from aaa order by xxx
union all
select yyy from bbb order by yyy

错误原因居然是,如果同时用了union all 和 order by,union all的子句要加上括号。

所以下面的写法就不会报错了。

(select xxx from aaa order by xxx)
union all
(select yyy from bbb order by yyy)