当union all和order by 如下一起使用时,会报sql未正确结束
select name,age from student order by age
union all
select name,age from person order by age
通用的解决方法有三种:
1、将结果集作为一张临时表然后查询排序
select * from (select name,age from student order by age
union all
select name,age from person order by age) order by age
2、单独对表进行排序后进行并集操作
select * from (select name,age from student order by age)
union all
select * from (select name,age from person order by age)
3、order by + 字段在结果集中的序号
select name,age from student
union all
select name,age from person
order by 2
本文探讨了在SQL中使用Union All与Order By时可能遇到的问题,并提供了三种有效的解决策略,包括创建临时表、预排序表再并集及使用结果集序号排序。
1860

被折叠的 条评论
为什么被折叠?



