#高階3:排序查詢
/* 語法:
select +要查的字段 +from +表名 +[where 條件]+order by + [排序列表]+【asc|desc】
asc 表示公升序,desc表示降序
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 * , salary*12*(1+ifnull(commission_pct,0)) as '年薪' from employees order by salary*12*(1+ifnull(commission_pct,0)) desc;
#案例:按年薪的高低顯示員工的資訊和年薪(按別名排列)
select * , salary*12*(1+ifnull(commission_pct,0)) as '年薪' from employees order by '年薪' desc;
#案例:按姓名的長度顯示員工的姓名和工資(按函式排列)
select last_name ,salary,length(last_name) as "長度" from employees order by length(last_name) desc;
#案例:查詢員工資訊,先按工資排序,再按員工編號排序【按多個字段排序】
select * from employees order by salary desc, employee_id desc;
#作業:查詢員工的姓名和部門號和年薪,按年薪降序,按姓名公升序
select last_name ,department_id,salary*12*(1+ifnull(commission_pct,0)) as '年薪' from employees order by '年薪' desc;
#作業:查詢員工的姓名和部門號和年薪,按姓名公升序
select last_name ,department_id,salary*12*(1+ifnull(commission_pct,0)) as '年薪' from employees order by last_name asc;
#作業:選擇工資不在8000到17000的員工姓名和工資,按工資降序
select last_name,salary from employees where not(salary between 8000 and 17000) order by salary desc;
#作業:查詢郵箱中帶e的員工資訊,先按郵箱的位元組數降序,再按部門號公升序。
select * from employees where email like '%e%' order by length(email) desc ,department_id asc;
mysql查詢字段排序 mysql 排序查詢字段
mysql 排序查詢字段 閱讀 504 排序查詢 語法 select 查詢欄位1 from 表 where 篩選條件 order by 要排序欄位2 asc公升序 desc 降序,要排字段3 asc公升序 desc降序 如果不寫預設為公升序 案例 查詢員工資訊,要求工資從高到低排序 select f...
MySQL 查詢排序
為了方便檢視資料,可以對資料進行排序 語法 select from 表名 order by 列1 asc desc 列2 asc desc,說明 將行資料按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推 預設按照列值從小到大排列 asc asc從小到大排列,即公升序 desc從大...
MySql 排序查詢
排序查詢 order by 排序列表 asc desc order by 子句中支援單個字段,多個字段,表示式,函式,別名 放在查詢語句的最後面,limit除外 案例1 查詢員工資訊,要求工資從高到低排序 select from employees order by salary desc 案例2 ...