關於order by的一些用法

來源:互聯網
上載者:User

關於order by的一些用法

1. order by 中關於null的處理

預設處理:Oracle在order by時認為null是最大值, 所以如果是asc升序則排在最後, desc降序則排在最前.

我們可以使用nulls first或者nulls last來控制null的位置

將null放在最前:select * from student order by name asc nulls first

將null放在最後:select * from student order by name desc nulls last

2. 幾種排序的寫法:

單列升序:select name from student order by name; (預設升序,即使不寫asc)
單列降序:select name from student order by name desc;
多列升序:select id, name from student order by id, name;
多列混合排序:select id, name from student order by id desc, name asc; (先按id降序,如果id相同則按name升序)

3. 強制某列排第一:

select * from student order by decode(id, 3, 1, 2), id;  // id為3的值排第一

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.