溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法

發(fā)布時間:2021-06-03 13:58:17 來源:億速云 閱讀:3950 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章將為大家詳細講解有關(guān)MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

數(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;

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法 

(2)、取得job 為 MANAGER 的員工,按照薪水由小到大排序(系統(tǒng)默 認由小到大)

例如: select ename,job,sal from emp where job = ”MANAGER”order by sal;

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法
如果包含 where 語句 order by 必須放到 where 后面,如果沒有 where 語句 order by 放到表的后面;

(3)、以下詢法是錯誤的:

select * from emp order by sal whereselect * from emp order by sal where job = ‘MANAGER';

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法

2、手動指定字段排序

(1)、手動指定按照薪水由小到大排序(升序關(guān)鍵字 asc)

例如: select ename,sal from emp order by sal asc;

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法 

(2)、手動指定按照薪水由大到小排序(降序關(guān)鍵字desc)

例如: select ename,sal from emp order by sal desc;

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法 

3、多個字段排序

(1)、按照 job 和薪水倒序排序

例如: select ename,job,ename from emp order by job desc,sal desc;

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法 

注意: 如果采用多個字段排序,如果根據(jù)第一個字段排序重復(fù)了,會根據(jù)第二個字段排序;

4、使用字段位置排序

(1)、按照薪水升序排序(不建議采用此方法,采用數(shù)字含義不明確,可讀性不強,程序不健壯)

select * from emp order by 6;

MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法

關(guān)于“MySQL中asc、desc數(shù)據(jù)排序的實現(xiàn)方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI