Oracle中的order by

本文介绍了Oracle数据库中NULL值的处理方式及排序技巧。详细解释了如何通过NULLS FIRST和NULLS LAST语法控制NULL值在升序或降序排序中的位置,并提供了多种排序实例,包括单列和多列的升序、降序以及混合排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、ORDER BY 中关于NULL的处理
缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前。
当然,你也可以使用nulls first 或者nulls last 语法来控制NULL的位置。
Nulls first和nulls last是Oracle Order by支持的语法
如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc 还是 desc)
如果Order by 中指定了表达式Nulls last则表示null值的记录将排在最后 (不管是asc 还是 desc)
使用语法如下:
--将nulls始终放在最前
select * from zl_cbqc order by cb_ld nulls first
--将nulls始终放在最后
select * from zl_cbqc order by cb_ld desc nulls last
2、几种排序的写法
单列升序:select<column_name> from <table_name> order by <column_name>; (默认升序,即使不写ASC)
单列降序:select <column_name> from <table_name> order by <column_name> desc;
多列升序:select <column_one>, <column_two> from <table_name> order by <column_one>, <column_two>;
多列降序:select <column_one>, <column_two> from <table_name> order by <column_one> desc, <column_two> desc;
多列混合排序:select <column_one>, <column_two> from <table_name> order by <column_one> desc, <column_two> asc;
3、新写法
SQL> select * from tb;
    BLOGID BLOGCLASS
---------- ------------------------------
        1 人生
        2 学习
        3 工作
        5 朋友
SQL> select * from tb order by decode(blogid,3,1,2), blogid;
    BLOGID BLOGCLASS
---------- ------------------------------
        3 工作
        1 人生
        2 学习
        5 朋友

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值