查詢(select)
select執(zhí)行順序
select 字段
from 表名
where ……
group by ……
having ……
order by ……
limit ……
?執(zhí)行順序
- 執(zhí)行where語句過濾原始數(shù)據(jù)
- 執(zhí)行g(shù)roup by進(jìn)行分組
- 執(zhí)行having對分組數(shù)據(jù)進(jìn)行操作
- 執(zhí)行select選出數(shù)據(jù)
- 執(zhí)行order by排序
- 執(zhí)行l(wèi)imit選出數(shù)據(jù)
若是連接查詢暖侨,則先經(jīng)過on產(chǎn)生中間表剧蚣,再經(jīng)過where進(jìn)行過濾提前,最后再由having進(jìn)行篩選
select相關(guān)
select * from emp; #與java等關(guān)聯(lián)時吗货,不建議使用*,不容易維護(hù)
select sal*12 as yearsal from emp; #as可將字段重命名
select ename,sal from emp; #多個字段用逗號隔開
1狈网、查詢條件
運算符 | 說明 |
---|---|
= | 等于 |
<>或!= | 不等于 |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
between...and... | 兩個值之間宙搬,包含邊界,等同于 >= and <= |
is null | 為null (is not null 不可空) |
and | 并且 |
or | 或者 |
in | 包含在內(nèi)(not in 不包含在內(nèi)) |
not | 取非拓哺,主要用在is或in中 |
like | 模糊查詢勇垛,支持%或匹配,%匹配任意個字符士鸥,一個下劃線匹配一個字符 |
2闲孤、排序
select * from emp order by deptno,job
;多個字段用逗號分開,先排前面再根據(jù)后面細(xì)排
select * from emp order by deptno asc;
asc表示升序烤礁,是默認(rèn)排序讼积,可不寫
select * from emp order by deptno desc;
desc表示降序
select * from emp order by 3;
不建議使用字段編號肥照,不健壯,不方便維護(hù)
3勤众、單行處理函數(shù)
單行處理函數(shù)舆绎,也叫數(shù)據(jù)處理函數(shù)
函數(shù) | 說明 |
---|---|
Lower | 轉(zhuǎn)換小寫 |
upper | 轉(zhuǎn)換大寫 |
left | 截取字符串左邊部分,left(字符串,長度) |
right | 截取字符串右邊部分们颜,right(字符串,長度) |
substr | 取子串(substr(被截取的字符串吕朵,起始下標(biāo),截取的長度)) |
length | 取長度 |
trim | 去空格窥突,可去除首尾空格努溃,不會去除中間空格。rtrim(去掉右側(cè))阻问,ltrim(去掉左側(cè)) |
str_to_date | 將字符串轉(zhuǎn)換成日期梧税,具體格式str_to_date (字符串,匹配格式) |
date_format | 格式化日期 |
format | 設(shè)置千分位则拷,format(數(shù)據(jù),小數(shù)位數(shù)) |
round | 四舍五入 round(數(shù)字,小數(shù)位數(shù)) |
rand() | 生成隨機(jī)數(shù) |
ifnull | 可以將null轉(zhuǎn)換成一個具體值ifnull(原始數(shù)據(jù),替換后的數(shù)據(jù)) |
concat() | 拼接字符串,concat(字段1,'字符串',字段2,'字符串') |
select lower(ename) from emp;//名字小寫
select upper(job) from emp;//job大寫
select * from emp where substr(ename,1,1)='M';//名字M開頭的員工
select length(ename) from emp;//名字長度
select * from emp where job=trim(' MANAGER ');//去除首位空格曹鸠,因此能查出結(jié)果
select * from emp where hiredate=str_to_date('02-20-1981',%m-%d-%Y);//查詢此日期入職員工
select empno, ename, date_format(hiredate, '%Y-%m-%d %H:%i:%s') as hiredate from emp;//將入職日期格式化
select empno, ename, Format(sal, 2) from emp;//薪水加入千分位并保留兩位小數(shù)
select round(123.56,1);//四舍五入煌茬,保留一位小數(shù)
select * from emp order by rand() limit 2;//隨機(jī)抽取記錄數(shù)
select ifnull(comm,0) from emp;//comm如果有null,替換為0
4彻桃、日期和時間
常用日期和時間處理函數(shù)
函數(shù) | 說明 |
---|---|
AddDate() | 增加一個日期(天坛善,周等),AddDate('日期',interval 數(shù)字 單位)或AddDate('日期',數(shù)字)--此時默認(rèn)為day |
AddTime() | 增加一個時間(時邻眷,分等)眠屎,AddTime('hh:mm:ss.xxxxx','hh:mm:ss.xxxxx') |
DateDiff() | 計算兩個日期之差 |
now() | 返回當(dāng)前日期和時間 |
CurDate() | 返回當(dāng)前日期 |
CurTime() | 返回當(dāng)前時間 |
Date() | 返回日期時間的日期部分 |
Time() | 返回日期時間的時間部分 |
Year() | 返回一個日期的年份部分 |
Month() | 返回一個日期的月份部分 |
Day() | 返回一個日期的天數(shù)部分 |
Hour() | 返回一個時間的小時部分 |
Minute() | 返回一個時間的分鐘部分 |
Second() | 返回一個時間的秒部分 |
DayOfWeek() | 對于一個日期,返回其星期幾 |
日期格式說明
日期格式 | 說明 |
---|---|
%Y | 4位年份 |
%y | 2位年份 |
%m | 月份(01......12) |
%c | 月份(1......12) |
%H | 小時(00......23) |
%h | 小時(01......12) |
%i | 分鐘(00......59) |
%S或%s | 秒(00......59) |
%r | 時間肆饶,12小時制(hh:mm:ss [AP]M) |
%T | 時間改衩,24小時制(hh:mm:ss) |
5、case語句
case...when...then...else...end
case具有兩種格式驯镊,簡單case函數(shù)與case搜索函數(shù)
a葫督、簡單case函數(shù)
?格式
??case 列名
??when 條件值1 then 選擇項1
??when 條件值2 then 選擇項2
??......
??else 默認(rèn)值 end
select ename,deptno,(case deptno when 10 then 'aaaa' when 20 then 'bbbb' else 'cccc' end) as col from emp;
b、case搜索函數(shù)
?格式
??case
??when 列名(=,<,>)條件值1 then 選擇項1
??when 列名(=,<,>)條件值2 then 選擇項2
??......
??else 默認(rèn)值 end
select ename,deptno,(case when deptno=10 then 'aaaa' when deptno=20 then 'bbbb' else 'cccc' end) as col from emp;
注意:Case函數(shù)只返回第一個符合條件的值板惑,剩下的Case部分將會被自動忽略
6橄镜、多行處理函數(shù)
多行處理函數(shù),又叫分組函數(shù)冯乘、聚合函數(shù)洽胶,聚合函數(shù)可在select中一起使用
函數(shù) | 說明 |
---|---|
count | 取得記錄數(shù) |
sum | 求和 |
avg | 求平均 |
max | 求最大值 |
min | 求最小值 |
注意:分組函數(shù)自動忽略空值,無需where排除裆馒,且分組函數(shù)不能使用在where關(guān)鍵字后面
select count(*) from emp;//取得記錄總數(shù)
select count(distinct(job)) from emp;//取得不同job數(shù)姊氓,distinct關(guān)鍵字可去重
select sum(sal+ifnull(comm,0)) from emp;//計算薪資總數(shù)丐怯,因為sum不計算含null記錄,因此要先判空
select avg(sal) from emp;//取得平均薪水
select max(sal) from emp;//取得最大薪水
select ename,min(str_to_date(hiredate,'%Y-%m-%d')) from emp;//取得最早入職員工
7他膳、分組查詢
涉及到分組查詢的主要是兩個關(guān)鍵字:group by與having
group by進(jìn)行分組
having對分組后的數(shù)據(jù)再進(jìn)行過濾
SQL語句中若有g(shù)roup by 語句响逢,那么在select語句后面只能跟分組函數(shù)+參與分組的字段
能在where中過濾的數(shù)據(jù),盡量在where中過濾棕孙,效率較高舔亭,having的過濾是專門對分組之后的數(shù)據(jù)進(jìn)行過濾的
select job,deptno,sum(sal) from emp group by job,deptno;//工作與部門進(jìn)行分組,計算工資和
8蟀俊、去重
使用關(guān)鍵字distinct去掉重復(fù)記錄
# 列出所有不同的職位
select distinct job from emp;
# 查出所有部分都設(shè)置有什么職位
select distinct deptno,job from emp; #distinct必須放在所有字段之前钦铺,表示后面字段不全部一致時的結(jié)果