數(shù)據(jù)排序 asc、desc
1、單一字段排序order by 字段名稱
作用: 通過哪個或哪些字段進行排序
含義: 排序采用 order by 子句,order by 后面跟上排序字段,排序字段可以放多個,多個采用逗號間隔,order by默認采用升序(asc),如果存在 where 子句,那么 order by 必須放到where 語句后面。
(1)、按照薪水由小到大排序(系統(tǒng)默認由小到大)
例如: select ename,sal from emp order by sal;
(2)、取得job 為 MANAGER 的員工,按照薪水由小到大排序(系統(tǒng)默 認由小到大)
例如: select ename,job,sal from emp where job = ”MANAGER”order by sal;
如果包含 where 語句 order by 必須放到 where 后面,如果沒有 where 語句 order by 放到表的后面;
(3)、以下詢法是錯誤的:
select * from emp order by sal whereselect * from emp order by sal where job = ‘MANAGER';
2、手動指定字段排序
(1)、手動指定按照薪水由小到大排序(升序關(guān)鍵字 asc)
例如: select ename,sal from emp order by sal asc;
(2)、手動指定按照薪水由大到小排序(降序關(guān)鍵字desc)
例如: select ename,sal from emp order by sal desc;
3、多個字段排序
(1)、按照 job 和薪水倒序排序
例如: select ename,job,ename from emp order by job desc,sal desc;
注意: 如果采用多個字段排序,如果根據(jù)第一個字段排序重復(fù)了,會根據(jù)第二個字段排序;
4、使用字段位置排序
(1)、按照薪水升序排序(不建議采用此方法,采用數(shù)字含義不明確,可讀性不強,程序不健壯)
select * from emp order by 6;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。