select 查詢列表
from 表
【where 篩選條件】
order by 排序列表 【asc|desc】
select
* from
employees
order by salary desc ;
從低到高是asc(預設是asc)
select
* from
employees
where department_id >= 90
order by hiredate asc ;
select
*,salary * 12 * (1+ ifnull(commission_pct, 0)) as 年薪
from
employees
order by 年薪 desc ;
select
length(last_name) as 位元組長度,
last_name,
salary
from
employees
order by 位元組長度 desc;
select
* from
employees
order by salary asc,
employee_id desc ;
測試題select
last_name,
department_id,
salary * 12 * (1+ ifnull(commission_pct, 0)) as 年薪
from
employees
order by 年薪 desc,
last_name asc ;
select
last_name,
salary
from
employees
where salary not between 8000
and 17000
order by salary desc ;
select
* from
employees
where email like '%e%'
order by length(email) desc,
department_id asc ;
4. 常見函式
呼叫:select 函式名(實參列表) 【from 表】;
特點:分類:
單行函式
select
concat(upper(last_name), lower(first_name)) as 姓名
from
employees ;
select
substr(
'李莫愁愛上了陸展元',
6) as output ;
select
substr(
'李莫愁愛上了陸展元',1,3
) output ;
select
concat(
upper(substr(last_name, 1, 1)),
'_',
lower(substr(last_name, 2))
) as output
from
employees ;
select
instr(
'楊不悔愛上了殷六俠',
'殷六俠'
) as output ;
select
length(trim(' 張翠山 ')) as output ;
select
trim('a' from 'aaa張a翠aa山aaaaa') as output ;
數學函式
日期函式
select
year(hiredate) 年
from
employees ;
select
str_to_date('1998-3-2', '%y-%c-%d') as output ;
select
* from
employees
where hiredate = str_to_date('4-3 1992', '%c-%d %y') ;
select
date_format(now(), '%y年%m月%d日)') as output ;
select
last_name,
date_format(hiredate, '%m月/%d日 %y年') as 入職日期
from
employees
where commission_pct is not null ;
其他函式
select version(); 當前資料庫伺服器的版本
select database(); 當前開啟的資料庫
select user(); 當前使用者
password('字元'); 返回該字元的密碼形式
md5('字元'); 也是加密的一種形式(md5)
select
last_name,
commission_pct,
if(commission_pct is null,
'沒獎金,呵呵',
'有獎金,嘻嘻'
) 備註
from
employees ;
語法:case 要判斷的字段或表示式
when 常量1 then 要顯示的值1或語句1;
when 常量2 then 要顯示的值2或語句2;
...else 要顯示的值n或語句n;
end查詢員工的工資,要求:
部門號=30,顯示的工資為1.1倍
部門號=40,顯示的工資為1.2倍
部門號=50,顯示的工資為1.3倍
其他部門,顯示的工資為原工資
select
salary as 原始工資,
department_id,
case
department_id
when 30
then salary * 1.1
when 40
then salary * 1.2
when 50
then salary * 1.3
else salary
end as 新工資
from
employees ;
case
when 條件1 then 要顯示的值1或語句1
when 條件2 then 要顯示的值2或語句2
...else 要顯示的值n或語句n
end查詢員工的工資情況
如果工資》20000,顯示a級別
如果工資》15000,顯示b級別
如果工資》10000,顯示c級別
否則,顯示d級別
select
salary,
case
when salary > 20000
then 'a'
when salary > 15000
then 'b'
when salary > 10000
then 'c'
else 'd'
end as 工資級別
from
employees ;
測試題
select now();
select
employee_id,
last_name,
salary,
salary * 1.2 as "new salary"
from
employees ;
select
last_name,
length(last_name)
from
employees
order by substr(last_name, 1, 1) ;
select
concat(
last_name,
' earns ',
salary,
' monthly but wants ',
salary * 3
) as "dream salary"
from
employees ;
select
last_name,
job_id as job,
case
job_id
when 'ad_pres'
then 'a'
when 'st_man'
then 'b'
when 'it_prog'
then 'c'
when 'sa_pre'
then 'd'
when 'st_clerk'
then 'e'
end as grade
from
employees
where job_id = 'ad_pres' ;
常見排序演算法和查詢演算法
時間複雜度 o n 2 穩定 let arr 3,15,9,10,1,26,2,5 for let i 0 i arr.length 1 i console.log arr 1,2,3,5,9,10,15,26 時間複雜度 o n 2 不穩定 let arr 3,15,9,10,1,26,2,5 f...
mysql查詢字段排序 mysql 排序查詢字段
mysql 排序查詢字段 閱讀 504 排序查詢 語法 select 查詢欄位1 from 表 where 篩選條件 order by 要排序欄位2 asc公升序 desc 降序,要排字段3 asc公升序 desc降序 如果不寫預設為公升序 案例 查詢員工資訊,要求工資從高到低排序 select f...
MySQL常見函式
注釋 字元函式 函式解釋 lower str 字母轉小寫 upper str 字母轉大寫 concat str,str,將多個字串拼接 substr str from pos for len 字母轉小寫 length str 獲取字串長度 instr str,substr 返回substr第一次出現...