查找
查找分為單表查詢與多表查詢
單表查詢
查看現(xiàn)有數(shù)據(jù)表
01現(xiàn)有數(shù)據(jù)庫.png
查看所有數(shù)據(jù)
-- select * from 表名; 查找指定表的所有數(shù)據(jù)
select * from article;
02查看所有數(shù)據(jù).png
查看某些字段
比如我只關心title 字段 或者 我只關心title 字段 和content_num 字段
-- select 字段名【,字段名湃崩,字段名...】 from 表名 ;
select title from article; -- 只關心title
select title,content_num from article; -- 只關心title 和 content_num
03查看指定字段.png
給字段起別名
如果將查出來的結果導出到文件,別人看到title 或者 content_num 可能不知道是什么意思接箫,那怎么解決這個問題攒读?答案是別名,什么是別名辛友?可以理解為昵稱薄扁,給字段起一個昵稱剪返,需要使用as 關鍵字。
select title as "標題" from article;
select title as "標題",content_num as "字數(shù)" from article;
04字段別名.png
注:別名只對當前語句有效
05別名的作用范圍.png
以上為SQL 的特殊語法邓梅,可以忽略脱盲,重點與我們的預期不符,標題是title 的別名日缨,但是沒得到相關的數(shù)據(jù)钱反。所以別名只對當前語句(起別名的語句)有效。
給表起別名
能給字段起別名匣距,能不能給表起別名面哥?答案是可以,驗證一下毅待。
select title as "標題" from article as arti;
select title as "標題",content_num as "字數(shù)" from article as arti;
06表別名.png
SQL條件語句
條件語句需要where 關鍵字尚卫,有哪些條件呢?
先處理下一下數(shù)據(jù)備用尸红。
alter table article modify content_file_path varchar(100) default "/article/details/80244167";
insert into article(title,content_num ) values("Python",1000),("python1",500),("python2",1000),("python3",1500),("python4",2000),("python5",2500),("python6",3000),
("python7",3500),("python8",4000),("python9",4500),("python10",5000),("python11",5500),
("python12",6000),("python13",6500),("python14",7000),("python15",7500),("python16",8000),
("python17",8500),("python18",9000),("python19",9500);
07處理數(shù)據(jù)備用.png
比較運算[1]
- 等于: =
-- 查看數(shù)據(jù)content_num 為4000的數(shù)據(jù)
-- select 字段名【,字段名焕毫,字段名...】 from 表名 【where 條件】;
select * from article where content_num =4000;
-- 查看數(shù)據(jù)content_num 為1000的整數(shù)倍的數(shù)據(jù)
-- 數(shù)值類型支持+、-驶乾、*邑飒、/、%
select * from article where content_num%1000 =0;
08等于.png
- 大于: >
-- content_num 大于8000
select * from article where content_num>8000;
09大于.png
- 大于等于: >=
-- content_num 大于等于8000
select * from article where content_num>=8000;
10大于等于.png
- 小于: <
-- content_num 小于8000
select * from article where content_num<8000;
11小于.png
- 小于等于: <=
-- content_num 小于等于8000
select * from article where content_num<=8000;
12小于等于.png
- 不等于: != 或 <>
-- content_num 不等于5201314级乐, <> 和 != 一樣所以只驗證一個
select * from article where content_num!=5201314;
13不等于.png
- 邏輯運算符
- and
-- and 數(shù)據(jù)的交集疙咸,只有and 左右兩邊的條件都滿足的數(shù)據(jù)才會被顯示出來
-- 最終結果取每個條件單獨成立時,數(shù)據(jù)記錄結果集的公共部分組成
-- title=YanglingWang content_num=5201314
select * from article where title="YanlingWang" and content_num="5201314";
14AND.png
- or
-- or 數(shù)據(jù)的并集风科,只要or 兩邊的條件有一邊成立就會有數(shù)據(jù)顯示
-- 最終結果由每個條件單獨成立時撒轮,數(shù)據(jù)結果集的去重集合組成
-- title=Python5 content_num=8000 **數(shù)值型的值也可以使用"引號"包裹
select * from article where title="Python5" or content_num="8000";
15OR.png
- not
-- not 數(shù)據(jù)的差集,或者取反贼穆,只有not 右邊的條件不滿足是才會有數(shù)據(jù)顯示
-- 最終結果由表中的數(shù)據(jù)記錄去除條件成立的結果集組成题山,既取反,對于表取條件成立的反結果集
-- 取所有content_num 不能被10 整除的數(shù)據(jù)
select * from article where not (content_num%10=0);
16NOT.png
模糊查詢
關鍵字 like
通配符%:表示任意多個任意字符
通配符_: 表示一個任意字符
-- 查詢所有title中含有W 字母的數(shù)據(jù)故痊,W前后可以有任意字符
select * from article where title like "%W%";
17like與%.png
-- 查詢所有content_num 中所有含幸運數(shù)字5 的數(shù)據(jù)顶瞳,并且5在百位
select * from article where CAST(title as varchar(20)) like "%5__";
18like與_.png
注
模糊不能對int 進行直接查找,所以使用cast[2] 對字段進行一個臨時轉化
怎樣查找含有% 或者_ 的字符串?使用 escape 轉義[3] 愕秫, ( like '/_fang' escape '/' ) 匹配 _fang
- 范圍查詢
- in表示在一個非連續(xù)的范圍內
- between ... and ...表示在一個連續(xù)的范圍內
-- in 多個獨立條件成立的并集
select * from article where title in("Python1","Python3","Python5");
-- between ... and ... 連續(xù)范圍慨菱,等價于 >= and <=
select * from article where content_num between 8000 and 10000;
19范圍查找.png
- 空判斷
- null 是一個特殊的值,不能使用= 判斷
- 判斷為空is null
- 判斷不為空
-- 查找title 不為null 并且content_num 小于2000 的數(shù)據(jù)
select * from article where (title is not null) and content_num < 2000;
-- 查找file_path 為null 的數(shù)據(jù)
select * from article where file_path is null;
select * from article where file_path = null;
select * from article where file_path = "null";
20判斷是否為Null.png
排序
- 關鍵字 order by
- 升序 asc :從小到大排序戴甩,默認asc排序
- 降序 desc : 從大到小排序
-- 查找content_num 大于 8000的數(shù)據(jù)符喝,按照content_num 從大到小排序
-- order by 字段 desc(asc)
select * from article where content_num >8000 order by content_num desc;
21排序.png
聚合函數(shù)[4]
- count(*) 統(tǒng)計有多少記錄,指定字段如果有null 值甜孤,不計數(shù)
select count(*) from article;
22count.png
- max(字段) 指定字段的最大值
- min(字段) 指定字段的最小值
-- 查看content_num 的最大值
select max(content_num ) from article;
-- 查看content_num 的最小值
select min(content_num ) from article;
23max_min.png
- sum(字段) 計算總和协饲,null 不計算
- avg(字段) 計算平均值,null 算入分母
-- 查看content_num 的總和
select max(content_num ) from article;
-- 查看content_num 的最小值
select min(content_num ) from article;
24sum_avg.png
- round() 四舍五入
-- 將平均值保留兩位小數(shù)
-- round(字段畏腕,小數(shù)位數(shù))
select round(avg(content_num),2) from article;
25round.png
分組
- group by 將查詢結果按照1個或多個字段進行分組,字段值相同的為一組
- group_concat() 顯示分組內數(shù)據(jù)
-- 按照content_num 分組茉稠,使用group_concat 顯示分組內的title,對結果集排序
select content_num,group_concat(title) from article group by content_num desc;
26group by 排序.png
- group by + 集合函數(shù)
- group by + having
-- 按照content_num 分組描馅,使用count 顯示每組的個數(shù)
select content_num,count(*) from article group by content_num ;
-- 按照content_num 分組,使用having 只顯示count >=2的數(shù)據(jù)
select content_num,count(*) from article group by content_num having count(*)>=2;
27group by 聚合函數(shù).png
分頁
- 關鍵字 limit
- limit start,count
-- 顯示部分行l(wèi)imit 開始位置,每頁顯示的內容數(shù)
-- 推算開始位置战惊,當每頁顯示內容固定時流昏,每一頁的開始位置=(頁數(shù)-1)*內容數(shù)
select * from article limit 0,5; -- 第一頁
select * from article limit 5,5; -- 第二頁
select * from article limit 10,5; -- 第三頁
27limit.png
到此結?DragonFangQy 2018.5.13