select * from 表名
where
group by
having
distinct
order by
limit a,b
between * and *
測試數據
# 測試數據
create table stu(id int primary key auto_increment,name char(10),math float,english float);
insert into stu values(null,"趙雲",90,30);
insert into stu values(null,"小喬",90,60);
insert into stu values(null,"小喬",90,60);
insert into stu values(null,"大喬",10,70);
insert into stu values(null,"李清照",100,100);
insert into stu values(null,"鐵拐李",20,55);
insert into stu values(null,"小李子",20,55);
1. where關鍵字
# where后面可以接 運算符
select *from stu where english > 60;
select *from stu where english < 60;
select *from stu where english = 60;
select *from stu where english <= 60;
select *from stu where english >= 60;
# where后面可以接 in ,not in
select *from stu where english in (60,70,80);
select *from stu where english not in (60,70,80);
# where后面可以接 邏輯運算符 and,or,not
select *from stu where english >= 60 and math >=60;
select *from stu where english >= 60 or math >=90;
select *from stu where not english >= 60; # 英語小於60分的記錄
select *from stu where not english >= 60 and math >=60; # 英語小於60 and 數學大於等於60
select *from stu where not (english >= 60 and math >=60); # 去掉 英語大於等於60 並且 數學大於等於60 的人。 換一句話說,只要有一門不及格就顯示出來。
# where后面可以接 模糊查詢 like % 表示 0個或多個。_表示一個任意字符
select * from stu where name like "小%";
# where后面可以接 BINARY 區分大小寫
select * from stu where BINARY name like "a%"; # 查詢以小寫字母a開頭的姓名
# 練習題1:查詢name以小開頭的名字,math < 80 並且 英語大於 20的人的數學成績
select math from stu where math < 80 and english >20 and name like "小%";
2. between * and *
select * from stu where english between 60 and 90;
3. distinct 去除重復記錄
# distinct 寫在 * 的前面
select distinct * from stu; # 只有 所有的字段 都存在重復的,才會去重
select distinct name from stu; # 只要 name 重復,就會去掉重復
4. group by
測試數據
create table emp (id int,name char(10),sex char,dept char(10),job char(10),salary double);
insert into emp values
(1,"劉備","男","市場","總監",5800),
(2,"張飛","男","市場","員工",3000),
(3,"關羽","男","市場","員工",4000),
(4,"孫權","男","行政","總監",6000),
(5,"周瑜","男","行政","員工",5000),
(6,"小喬","女","行政","員工",4000),
(7,"曹操","男","財務","總監",10000),
(8,"司馬懿","男","財務","員工",6000);
簡單練習:
1.查詢每個部門有幾個人
2.計算每個部門的平均工資
3.計算每個崗位的平均工資
4.計算每個部門每個崗位的平均工資
5.查詢平均工資大於5000的部門
# gourp 是分組的意思,即將一個整體按照某個特征或依據來分為不同的部分
# 為什么會有分組? 為了統計,例如統計男性有幾個,女性有幾個
# 統計函數
# 也稱為聚合函數,就是將一堆數據經過計算得出一個結果
# 練習題
# 求最大工資的姓名。
select name from emp where salary = max(salary); # 這個sql執行報錯,max()需要遍歷完所有的結果。where 字句 后面並沒有遍歷完,所以聚合函數不能放到where的后面
# 聚集函數
AVG() # 求平均數
SUM() # 求和
MAX() # 求最大值
MIN() # 求最小值
COUNT() # 在查詢某個字段有多少行時,null是不計入count的。
# 如何使用group by
select [字段1,...,字段n|*] from 表名 group by 字段1,...,字段n
# 容易出現問題
select * from emp group by dept;
mysql5.6 下查詢的結果是name僅顯示該分組下的一個數據。(按理應該報錯,但是在5.6之后,開啟了模糊模式。我們可以改成嚴格模式)
# 練習題
1. 查詢出部門人數小於3個人的部門名稱,人員姓名,具體的人數
mysql> select dept,group_concat(name),count(dept)as person_nums from emp group by dept having person_nums <3;
+--------+--------------------+-----+
| dept | group_concat(name) | person_nums |
+--------+--------------------+-----+
| 財務 | 曹操,司馬懿 | 2 |
+--------+--------------------+-----+
1 row in set (0.28 sec)
# as 去別名,可以省略
select name, english + math as 總分 from stu;
select name, english + math 總分 from stu;
having # 過濾
# 作用:
"""
1. 用於對分組后的數據進行刷選
2. 作用跟where相同,用於過濾
"""
# having與where的不同點
"""
1. 不同點在於where是從文件讀取數據時的過濾條件,這導致了where中不能使用聚合函數
2. having是分組后進行的過濾。
"""
# 為什么 不分組的時候在select 前面可以使用聚合函數呢?
"""
select SUM(salary) from where ...;
因為你where比select 后面的字段篩選更早執行 此時數據全都已經讀取了 所以可以進行統計
"""
# 練習題
"""
1. 查詢 崗位平均薪資高於6000的 崗位名稱和平均薪資
select dept,avg(salary) from emp group by dept having avg(salary) > 6000 ;
2. 查詢 部門人數少於3的 部門名稱 人員名稱 人員個數
select dept,group_concat(name),count(*) from emp group by dept having count(name) < 3;
"""
5. 聚合函數:
# 用戶處理字符串的函數
concat(str1,str2,str3…) # 合並字符串函數
strcmp(str1,str2) # 比較字符串大小函數
length(str) # 獲取字符串字節數函數
char_length(str) # 獲取字符串字符數函數
6. order by
select 字段1,...字段n from 表名 order by 字段名1,...字段名n; # 默認asc升序
select 字段1,...字段n from 表名 order by 字段名1,...字段名n desc; # 降序
select 字段1,...字段n from 表名 order by 字段名1 desc,...字段名n asc; # 字段1降序,字段n升序
7. limit
# 模式
"""
select 字段1,...字段n from 表名 order by 字段名1,...字段名n limit 10;
"""
# 作用
"""
1. 用於限制顯示的記錄數
2. limit [start,] count;
start 開始位置,第一行,start為0
count 顯示條數
不指定start時,則從第一條開始顯示
3. limit可用於分頁
分頁原理:先查詢總數據條數,設為a
確定每頁數量b,
總頁數為c = a / b 如果除不盡則需要加1 例如 10 / 3 正確頁數為4
查詢語句的起始位置為s = 當前頁數d 減去1 乘以每頁數量
即 s = (d - 1) * b
語句為:select*from table_name limit s,b
"""
# 練習
"""
1. 查看前三人
select *from emp limit 3;
2. 查看工資最高的那個人信息
select *from emp order by salary desc limit 1;
3. 指定起始位置,查看id為3-6的人
select *from emp limit 2,4;
"""