一躏敢、聚合函數(shù)
????1闷愤、為了快速得到統(tǒng)計數(shù)據(jù),經(jīng)常會用到如下5個聚合函數(shù)
????????count(*)表示計算總行數(shù)件余,括號中寫星與列名讥脐,結(jié)果是相同的
????????例1:查詢學(xué)生總數(shù)
????????select count(*) from students;
????2、max(列)表示求此列的最大值
????????例2:查詢女生的編號最大值
????????select max(id) from students where gender=0;
????3啼器、min(列)表示求此列的最小值
????????例3:查詢未刪除的學(xué)生最小編號
????????select min(id) from students where isdelete=0;
????4旬渠、sum(列)表示求此列的和
????????例4:查詢男生的編號之和
????????select sum(id) from students where gender=1;
????5、avg(列)表示求此列的平均值
????????例5:查詢未刪除女生的編號平均值
????????select avg(id) from students where isdelete=0 and gender=0;
二端壳、分組
????按照字段分組告丢,表示此字段相同的數(shù)據(jù)會被放到一個組中
????分組后,分組的依據(jù)列會顯示在結(jié)果集中损谦,其他列不會顯示在結(jié)果集中
????可以對分組后的數(shù)據(jù)進(jìn)行統(tǒng)計岖免,做聚合運算
????語法:
????select 列1,列2,聚合... from 表名 group by 列1,列2...
????例1:查詢男女生總數(shù)
????select gender as 性別,count(*)
????from students
????group by gender;
????例2:查詢各城市人數(shù)
????select hometown as 家鄉(xiāng),count(*)
????from students
????group by hometown;
????分組后的數(shù)據(jù)篩選
????語法:
????select 列1,列2,聚合... from 表名
????group by 列1,列2,列3...
????having 列1,...聚合...
????having后面的條件運算符與where的相同
????例3:查詢男生總?cè)藬?shù)
????方案一
????select count(*)
????from students
????where gender=1;
????-----------------------------------
????方案二:
????select gender as 性別,count(*)
????from students
????group by gender
????having gender=1;
????對比where與having
????where是對from后面指定的表進(jìn)行數(shù)據(jù)篩選,屬于對原始數(shù)據(jù)的篩選
????having是對group by的結(jié)果進(jìn)行篩選
三照捡、排序
????為了方便查看數(shù)據(jù)颅湘,可以對數(shù)據(jù)進(jìn)行排序
????語法:
????????select * from 表名
????????order by 列1 asc|desc,列2 asc|desc,...
????將行數(shù)據(jù)按照列1進(jìn)行排序,如果某些行列1的值相同時麻敌,則按照列2排序栅炒,以此類推
????默認(rèn)按照列值從小到大排列
????asc從小到大排列,即升序
????desc從大到小排序术羔,即降序
????????例1:查詢未刪除男生信息赢赊,按學(xué)號降序
????????select * from students
????????where gender=1 and isdelete=0
????????order by id desc;
????????例2:查詢未刪除科目信息,按名稱升序
????????select * from subjects
????????where isdelete=0
????????order by name;
四级历、獲取部分行
????當(dāng)數(shù)據(jù)量過大時释移,在一頁中查看數(shù)據(jù)是一件非常麻煩的事情
????語法
????select * from 表名
????limit start,count
????從start開始,獲取count條數(shù)據(jù)
????start索引從0開始
????????例1:查詢前3行男生信息
????????select * from students
????????where gender=1
????????limit 0,3;
????示例:分頁
????????已知:每頁顯示m條數(shù)據(jù)寥殖,當(dāng)前顯示第n頁
????????求總頁數(shù):此段邏輯后面會在python中實現(xiàn)
????????查詢總條數(shù)p1
????????使用p1除以m得到p2
????????如果整除則p2為總數(shù)頁
????????如果不整除則p2+1為總頁數(shù)
????????求第n頁的數(shù)據(jù)
????????select * from students
????????where isdelete=0
????????limit (n-1)*m,m
五玩讳、連接查詢
? ? 1、當(dāng)查詢結(jié)果的列來源于多張表時嚼贡,需要將多張表連接成一個大的數(shù)據(jù)集熏纯,再選擇合適的列返回
? ? 2、mysql支持三種類型的連接查詢粤策,分別為:
????????內(nèi)連接查詢:查詢的結(jié)果為兩個表匹配到的數(shù)據(jù)
????????左連接查詢:查詢的結(jié)果為兩個表匹配到的數(shù)據(jù)樟澜,左表特有的數(shù)據(jù),對于右表中不存在的數(shù)據(jù)? ? ? ? ? 使用null填充
????????右連接查詢:查詢的結(jié)果為兩個表匹配到的數(shù)據(jù),右表特有的數(shù)據(jù)秩贰,對于左表中不存在的數(shù)據(jù)? ? ? ? ? 使用null填充
????????語法
????????????select * from 表1
????????????inner或left或right join 表2 on 表1.列=表2.列
? ? 3霹俺、例1:使用內(nèi)連接查詢班級表與學(xué)生表
????????此處使用了as為表起別名,目的是編寫簡單
????????select * from classes as cls
????????inner join students as stu on sub.id=stu.cls.id
? ? 4毒费、例2:使用左連接查詢班級表與學(xué)生表
????????select * from classes as cls
????????left join students as stu on sub.id=stu.cls.id
? ? 5丙唧、例3:使用右連接查詢班級表與學(xué)生表
????????select * from classes as cls
????????right join students as stu on sub.id=stu.cls.id
? ? 6、例4:查詢學(xué)生姓名及班級名稱
????????select stu.name as stuname,cls.name as clsname from classes as cls
????????inner join students as stu on sub.id=stu.cls.id