Tag Archives: mysql Incorrect usage of UNION and ORDER BY

MYSQL Incorrect usage of UNION and ORDER BY [How to Solve]

The statement with this error is invalid

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

The reason for the error is that if you use union all and order by at the same time, the clause of union all needs to add brackets

So the following writing will not be reported error

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