union all中使用order by子查询时需将含order by的子查询包在一个不含order by的查询里再进行union all。
否则会报错“[Err] 1221 - Incorrect usage of UNION and ORDER BY”
例如:
1.错误用法:
select id from test1 order by id
union all
select id from test2 order by id
2.正确用法:
select * from (select id from test1 order by id) t1
union all
select * from (select id from test2 order by id) t2