Oracle中union 和 order by 的联用

1, 创建测试表

create table

 t
 as   
select 'china' col_1,'america' col_2,'canada' col_3,-1 status from dual
union all   
select '花生','瓜子','绿豆',0 from dual
union all   
select '牙膏','牙刷','杯子',3 from dual
union all   
select '芍药','牡丹','月季',1 from dual
union all   
select '优乐美','香飘飘','炸鸡',2 from dual; 

2, 在一张表中测试


--可以在一张表中测试
select * from (          
select col_1,col_2,col_3,status          
from t          
where status >= 0          
order by status)  
union all   
select * from (          
select col_1,col_2,col_3,status          
from t          
where status < 0          
order by status
);

 

2,  在多张表中做测试
--union跨表排序查询
select total.* from
(
   select t.* from (select col_1, col_2, col_3, status  from t order by status) t
   union all
   select t2.* from (select col_1, col_2, col_3, status  from t2 order by status) t2
) total
order by total.status;

 推荐学习网页网址: http://blog.csdn.net/bobo12082119/article/details/6323727#plain

有个问题:

                 项目中需要跨表排序查询的时候是不是用第二种测试方法?

                 如果不是

                 那有没有更好得实现跨表排序查询的方法?

                 如果有人知道请留言,很愿意跟大家交流,共同学习!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值