本篇文章博主將帶大家一起學習mysql中常用的數據查詢語言。
dql(data query language 數據查詢語言)
select 語法
1
2
3
4
5
6
7
8
9
|
select [ all | distinct ] { * | table .* | [ table .field1 [ as alias1][, table .field2] [ as alias2][,...]]} from table_name [ as table_alias] [ left | out | inner join table_name2] #聯合查詢 [ where ...] #指定結果需要滿足的條件 [ group by ...] #指定結果按照哪幾個字段來分組 [ having ...] #過濾分組的記錄必須滿足的次要條件 [oder by ...] #指定查詢記錄按一個或者多個條件排序 [limit [偏移量,返回結果記錄數]; #指定查詢的記錄從哪條至哪條 |
基本查詢語句&as
以下例子用student表中的數據。
查詢表中所有數據列結果,采用 “*” 符號,效率低
1
|
select * from student; |
可指定查詢列,效率高
1
|
select studentname,phone from student; |
as 子句的作用和用法
注意:as 可省略不寫
(1)可給數據列取一個新別名
1
|
select studentname as '學生姓名' from student; |
(2)給表取別名
1
|
select stu.address from student as stu; |
(3)可把計算或總結的結果用另一個新名稱來代替
1
|
select phone +1 as tel from student; |
distinct 關鍵字
作用:去掉select查詢出來的重復值(當所有返回值都相同時,只返回一條記錄)
語法:
1
|
select distinct 字段名1,字段名2,... from 表名 |
注意:all 關鍵字是默認的,返回所有記錄,與之相反
當過濾多列重復值時,只有當選擇過濾的列都存在重復值才進行過濾
1
|
select distinct studentno,address from student; |
過濾單列:查詢學生表中的地址
1
|
select distinct address from student; |
sql語句中的表達式
1
2
3
|
select version(),100*3 as 乘積; #返回mysql 版本和計算結果 select now() '當前時間' ; #返回當前時間 |
避免sql返回結果中包含“.” ,“*”,和括號等干擾開發語言程序
1
2
|
select version() as mysql_v,12.3*100 as expression; #返回結果不會與后臺開發程序發生混淆 |
拼接 concat
1
|
select concat(studentname, '@.com' ) as email from student; |
數值類型相加
1
|
select studentno+100 from student; |
比較運算符&通配符
where條件語句:用于檢索數據表中符合條件的記錄
搜索條件可以由一個或多個邏輯表達式組成,結果一般為真或假
搜索條件的組成:邏輯操作符、比較操作符
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# where 條件語句 select * from student where address= '四川' ; #查詢學生表中電話號碼不為空的學員姓名 select studentname from student where phone is not null ; #查詢學生表中電話號碼為空的學員姓名 select studentname from student where phone is null ; #查詢剛刪掉的數據——空值 select studentname from student where phone = '' ; # between and 適用于時間范圍 |
邏輯操作符
比較操作符
使用 like 關鍵字進行模糊查詢
- 與“%”一起使用,表示匹配0個或任意個字符
- 與“_”一起使用 表示匹配單個字符
1
2
3
4
5
6
|
#查詢學生表中姓張*的學生姓名 select studentname from student where studentname like '張_' ; select studentname from student where studentname like '%麗%' ; # in select * from student where address in ( '四川' , '上海' ); |
注意:
- 數值數據類型的記錄之間才能進行算數運算
- 相同的數據類型的數據之間才能進行比較
null
- null 代表“無值”
- 區別于零值0和空字符串“ ”
- 只能出現在定義允許為null的字段
- 須使用 is null 或 is not null 比較操作符去比較
內連接&自查詢
如果需要多張數據表的數據進行查詢,則可以通過連接運算符實現多個查詢。
分類包括:
-
內連接(inner jion):
- 等值和非等值的連接查詢
- 自身連接查詢
-
外連接(out jion)
- 左連接(left jion)
- 右連接(right jion)
order by 排序查詢
對select 語句查詢得到的結果,按某些字段進行排序
與desc(降序)或asc(升序)搭配使用,默認為asc
以subject表和grade表數據為例:
1
2
|
select * from subject order by classhour; #升序 select * from subject order by classhour desc ; #降序 |
多字段排序:先按照第一個字段排序,再按照第二個字段排序。如果第一個字段數據相同,再按照第二個字段排序。
1
|
select * from subject order by classhour,gradeid; |
limit分頁
limit [m,] n 或 limit n offset m
限制select返回結果的行數
m為第一個返回記錄行的偏移量
n返回記錄行的數目
注意:
- m不指定,則偏移量為0,從第一條開始返回前n條記錄
- limit 常用于分頁顯示
- 如果超出表中數據,則顯示全部
例如:
1
2
|
select * from grade limit 3; #返回前3條記錄 select * from grade limit 1,3; #返回2~4條記錄 |
總記錄數:total
1
|
select count (subjectno) '總數據' from subject; |
總頁數:int totalpage = total % pagesize ==0 ? total / pagesize : total / pagesize + 1
子查詢
在查詢語句where 條件子句中,又嵌套了另外一個查詢語句
注意:子查詢返回的結果一般是集合,建議使用in關鍵字
1
2
3
|
select subjectname from subject where gradeid in ( select gradeid from grade); |
聚合函數
常用的統計函數:count()、sum()、avg()、max()、min()
1
2
3
4
5
|
select count (studentno) '總數據' from student; select sum (classhour) '總學時' from subject; select avg (classhour) '平均學時' from subject; select max (classhour) '最長學時' from subject; select min (classhour) '最短學時' from subject; |
分組 group by
使用 group by 關鍵字對查詢結果分組
- 對所有的數據進行分組統計
- 分組的字段可以有多個,并依次分組
- 與 having 結合使用,進行分組后的數據篩選
以 student 表為例
(1)對student 表按照地址分組統計 group by
1
|
select address, count (address) from student group by address; |
having 過濾分組的記錄必須滿足的次要條件
(2)對 student 表 按照地址分組,滿足地址=1的 having
1
|
select group_concat(studentname), count (address) from student group by address having count (address)=1; |
合并 union 、union
- allunion #合并完全相同數據
- union all #合并所有數據
注意:合并兩張表時,列數必須一樣才能合并。
兩表列數不同時,會報以下錯誤:
[sql]select * from grade union select * from student;
[err] 1222 - the used select statements have a different number of columns
(1)合并 subject 表和 student表
1
2
|
select * from subject union select * from student; select * from subject union all select * from student; |
子查詢 exists ——>true false
exists / not exists 子查詢條件成立則顯示父查詢的結果,否則不顯示結果
(1)子查詢條件為真 (grade 表中 gradeid 1~5,存在 1)
1
2
3
|
select subjectname,gradeid from subject where exists ( select * from grade where gradeid=1); |
或
1
2
3
|
select subjectname,gradeid from subject where not exists ( select * from grade where gradeid=999); |
(2)子查詢條件為假 (grade 表中 gradeid 1~5,不存在 999)
1
2
3
|
select subjectname,gradeid from subject where exists ( select * from grade where gradeid=999); |
或
1
2
3
|
select subjectname,gradeid from subject where not exists ( select * from grade where gradeid=1); |
子查詢(any & all)
any :判斷條件中,若子查詢中任意一個值滿足條件,則執行父查詢
all :判斷條件中,若子查詢中所有值滿足條件,則執行父查詢
(1)滿足條件:存在 subject.gradeid >= grade.gradeid ,執行父查詢
1
2
3
|
select subjectname,gradeid from subject where gradeid >= any ( select gradeid from grade); |
(2)不滿足條件:所有subject.gradeid >= grade.gradeid ,不執行父查詢
1
2
3
|
select subjectname,gradeid from subject where gradeid >= all ( select gradeid from grade); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_43817064/article/details/97936194