您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页对union all中的查询结果进行排序

对union all中的查询结果进行排序

来源:爱玩科技网

1、对union all中的每个查询分别排序

现在有A表的数据和B表的数据,现需要将两个表的数据按照一定的条件进行查询之后,将两个结果集并起来,而且查询之后的数据是不会混在一起的,结果集先是A表后是B表。

另外,如果对这两个表的查询数据的排序条件不一样,需要分别排序,那么相关代码如下:

1)如果这种写法就会报错:

select a,b from tablea where … order by a
union all
select a,b from tableb where … order by b,c

2)试着改成下面的两种写法,就不会报错了:

第一种:
select * from(select a,b from tablea where … order by a)m
union all
select * from(select a,b from tableb where … order by b,c)n
第二种:
select * from(select TOP 100 Percent a,b from tablea where … order by a)m
union all
select * from(select TOP 100 Percent a,b from tableb where … order by b,c)n

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务