DQL語言 排序查詢

2021-09-05 09:53:55 字數 1794 閱讀 9014

查詢員工資訊,要求工資從高到低排序,使用order by

降序

select * from employees order by salary desc;

公升序

select * from employees order by salary asc;

注意: 如果不寫,預設是公升序

查詢部門編號》=90的員工資訊,按照入職時間的先後進行排序

select *

from employees

where department_id>=90

order by hiredate asc;

按年薪的高低,顯示員工的資訊和年薪[按表示式排序]

select ,salary12*(1+ifnull(commission_pct,0)) 年薪

from employees

order by salary12(1+ifnull(commission_pct,0)) desc;

或者 order by 後面可以跟別名

select ,salary12*(1+ifnull(commission_pct,0)) 年薪

from employees

order by 年薪 desc;

按照姓名的長度顯示員工的姓名和工資 函式length()

select length(last_name) 位元組長度,last_name, salary

from employees

order by length(last_name) desc;

查詢員工資訊,要求按工資公升序,再按員工編號降序

select * from employees

order by salary asc, employee_id desc;

注意: oder by 子句可以支援單個字段,多個字段,表示式,函式,別名,

order by子句一般是放在查詢語句的最後面,limit子句除外

案例

查詢員工的姓名和部門號和年薪,按年薪降序,按姓名公升序

select last_name,department_id,salary12(1+ifnull(commission_pct,0)) 年薪

from employees

order by 年薪 desc,last_name asc;

選擇工資不在 8000到17000的員工的姓名和工資,按工資降序

select last_name,salary

from employees

where salary not between 8000 and 17000

order by salary desc;

查詢郵箱中包含e的員工資訊,並按郵箱的位元組數降序,再按部門號公升序

select *,length(email)

from employees

where email like 「%e%」

order by length(email) desc,department_id asc

DQL語言的查詢

分類 一 按條件表示式篩選 條件運算子 二 按邏輯表示式篩選 邏輯運算子 and or not 三 模糊查詢 like between and in is null或is not null 語法 select 查詢列表 3 from 表名 1 where 條件 2eg 1 查詢工資大於10000的員...

DQL語言 基礎查詢

一,語法 select 查詢列表 from 表名 二,特點 1,查詢列表可以是字段,常量,表示式,函式 2,查詢結果是虛擬表 示例 1,查詢單個字段 select 欄位名 from 表名 select department id from departments 2,查詢多個字段 select 欄位...

DQL語言 基礎查詢

select 查詢列表 from 表名 1 查詢列表可以是字段 常量 表示式 函式,也可以是多個 2 查詢結果是乙個虛擬表 1 查詢單個字段 select 欄位名 from 表名 2 查詢多個字段 select 欄位名1,欄位名2,from 表名 3 查詢所有字段 select from 表名 4 ...