MySQL排序查詢

2021-10-09 23:16:12 字數 3378 閱讀 7908

按照工資公升序,找出員工名和薪資

預設公升序排列

[bjpowernode]

>select #找出

-> ename,sal #員工名,薪資

-> from

-> emp #表名

-> order by #排序關鍵字

-> sal;

#通過這個排序

+--------+---------+

| ename | sal |

+--------+---------+

| smith | 800.00 |

| james | 950.00 |

| adams | 1100.00 |

| ward | 1250.00 |

| martin | 1250.00 |

| miller | 1300.00 |

| turner | 1500.00 |

| allen | 1600.00 |

| clark | 2450.00 |

| blake | 2850.00 |

| jones | 2975.00 |

| scott | 3000.00 |

| ford | 3000.00 |

| king | 5000.00 |

+--------+---------+

14 rows in

set(0.00 sec)

指定公升序或者降序

1. asc:表示公升序

2. desc:表示降序

[bjpowernode]

>select ename,sal from emp order by sal asc;

#公升序排列

+--------+---------+

| ename | sal |

+--------+---------+

| smith | 800.00 |

| james | 950.00 |

| adams | 1100.00 |

| ward | 1250.00 |

| martin | 1250.00 |

| miller | 1300.00 |

| turner | 1500.00 |

| allen | 1600.00 |

| clark | 2450.00 |

| blake | 2850.00 |

| jones | 2975.00 |

| scott | 3000.00 |

| ford | 3000.00 |

| king | 5000.00 |

+--------+---------+

14 rows in

set(0.00 sec)

[bjpowernode]

>select ename,sal from emp order by sal desc;

#降序排列

+--------+---------+

| ename | sal |

+--------+---------+

| king | 5000.00 |

| scott | 3000.00 |

| ford | 3000.00 |

| jones | 2975.00 |

| blake | 2850.00 |

| clark | 2450.00 |

| allen | 1600.00 |

| turner | 1500.00 |

| miller | 1300.00 |

| ward | 1250.00 |

| martin | 1250.00 |

| adams | 1100.00 |

| james | 950.00 |

| smith | 800.00 |

+--------+---------+

14 rows in

set(0.01 sec)

按照工資降序排列,工資一樣的按照名字的公升序排列

[bjpowernode]

>select ename,sal from emp order by sal desc,ename asc;

+--------+---------+

| ename | sal |

+--------+---------+

| king | 5000.00 |

| ford | 3000.00 |

| scott | 3000.00 |

| jones | 2975.00 |

| blake | 2850.00 |

| clark | 2450.00 |

| allen | 1600.00 |

| turner | 1500.00 |

| miller | 1300.00 |

| martin | 1250.00 |

| ward | 1250.00 |

| adams | 1100.00 |

| james | 950.00 |

| smith | 800.00 |

+--------+---------+

14 rows in

set(0.00 sec)

# 多個字段同時排序,越靠前的位置起的作用越大

找出工作崗位是salesman的員工,並且要求按照薪資的降序排列

[bjpowernode]

>select #最後查

-> ename,job,sal

-> from #先執行

-> emp

-> where #接下來

-> job=

'salesman'

-> order by #最後排序輸出

-> sal desc;

+--------+----------+---------+

| ename | job | sal |

+--------+----------+---------+

| allen | salesman | 1600.00 |

| turner | salesman | 1500.00 |

| ward | salesman | 1250.00 |

| martin | salesman | 1250.00 |

+--------+----------+---------+

4 rows in

set(0.00 sec)

mysql查詢字段排序 mysql 排序查詢字段

mysql 排序查詢字段 閱讀 504 排序查詢 語法 select 查詢欄位1 from 表 where 篩選條件 order by 要排序欄位2 asc公升序 desc 降序,要排字段3 asc公升序 desc降序 如果不寫預設為公升序 案例 查詢員工資訊,要求工資從高到低排序 select f...

mysql 排序查詢

高階3 排序查詢 語法 select 要查的字段 from 表名 where 條件 order by 排序列表 asc desc asc 表示公升序,desc表示降序 order by 可以支援單個字段,多個字段,函式,表示式,別名。案例 查詢員工工資從高到低排列的員工資訊 select from ...

MySQL 查詢排序

為了方便檢視資料,可以對資料進行排序 語法 select from 表名 order by 列1 asc desc 列2 asc desc,說明 將行資料按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推 預設按照列值從小到大排列 asc asc從小到大排列,即公升序 desc從大...