聚合函數(shù)
聚合函數(shù)對(duì)一組值執(zhí)行計(jì)算,并返回單個(gè)值。 除了 COUNT 以外,聚合函數(shù)都會(huì)忽略空值。 聚合函數(shù)經(jīng)常與 SELECT 語(yǔ)句的 GROUP BY 子句一起使用。常用的聚合函數(shù)有:
- sum(expr):求和
- avg(expr):求平均數(shù)
- count(expr):計(jì)數(shù)器,返回SELECT語(yǔ)句檢索到的行中非NULL值的數(shù)目
- max(expr) 獲取最大值
- min(expr) 獲取最小值
mysql查詢子句
where(條件查詢)
在where條件查詢中可以使用比較運(yùn)算符,邏輯運(yùn)算符,in,between and和模糊查詢:
- 比較運(yùn)算符包括:>, =, <, >=, <=, !=
- 邏輯運(yùn)算符:and, or, not
- in:類似python中的in
- between and:表示在什么之間
- 模糊查詢like:類似正則表達(dá)式的用法,用通配符去匹配字符,但是在這里的通配符只有兩個(gè),%匹配任意長(zhǎng)度的任意字符,_匹配任意單個(gè)字符。
例如:
1.學(xué)生成績(jī)?yōu)?0分到90分之間的有哪些?
where scores between 60 and 90
2.查找學(xué)生姓王的學(xué)生?
where name like '王%'
group by(分組)
將查詢的結(jié)果按照某個(gè)字段的值進(jìn)行分組,如果需要對(duì)分組后的數(shù)據(jù)進(jìn)行篩選就還需要用到having子句,而不是where子句。
例如:
查找數(shù)學(xué)英語(yǔ)這兩門課的平均成績(jī)大于70分的學(xué)生:(假設(shè)表scores中有數(shù)學(xué)英語(yǔ)成績(jī)和姓名)
select name,avg(score) as c from scores group by(name) having c >70;
as(別名)
對(duì)于一些名字比較長(zhǎng)的字段或者表名,我們可以給它一個(gè)比較簡(jiǎn)短且全局唯一的別名,然后通過別名去引用它(查詢語(yǔ)句也可以做別名)。
例如:
select name,age from student as st;
select avg(age) as avg_age from student;
order by(排序)
通常在處理數(shù)據(jù)或者查詢數(shù)據(jù)的時(shí)候往往都需要進(jìn)行排序,sql中使用order by對(duì)相應(yīng)的數(shù)據(jù)進(jìn)行排序,默認(rèn)查詢結(jié)果是按照(asc)升序排列的,要降序排列使用desc。
例如:
查詢學(xué)生的成績(jī),并按照降序排列
select name,score from scores order by score desc;
if(判斷)
if(字段,exp1,exp2) 或者 ifnull(字段,exp1,,exp2) 作用:if表達(dá)式中如果字段值為真則返回exp1的值,如果為假的話,返回exp2的值 ifnull表達(dá)式中如果字段的值為假則返回exp1的值,如果為假的話,返回exp2的值。
例如: 查詢男女學(xué)生的人數(shù)(分組和聚合函數(shù))
select if(stusex, '男', '女') as '性別', count(stusex) as '人數(shù)' from TbStudent group by stusex;
去重和顯示結(jié)果條數(shù)
使用distinct(字段名)表示去除字段名中重復(fù)的項(xiàng)。
limit子句用來(lái)現(xiàn)在結(jié)果的顯示條數(shù),用法為limit [offset],N其中offset可以不設(shè)置默認(rèn)為0,如果設(shè)置則表示偏移offset條信息然后顯示N條信息。offset也可以單獨(dú)使用。
例如:
取出成績(jī)表中在第四個(gè)到第六個(gè)這三個(gè)人:
select name from scores limit 3,3;
子查詢
- where型子查詢
把內(nèi)層查詢結(jié)果當(dāng)作外層查詢的比較條件
例如:
不適用排序,找出最高分的學(xué)生姓名和成績(jī)
select name,score from scores where score = (select max(score) from scores);
- from型子查詢
把內(nèi)層的查詢結(jié)果供外層再次查詢
例如:
不用group by,查詢平均成績(jī)大于等于90分的學(xué)生的學(xué)號(hào)和平均成績(jī):
select ts1.stuid,t2.avg from tbstudent ts1,(select sid,avg(score) as avg from tbsc group by(sid)) as t2 where t2.sid=ts1.stuid and t2.avg>=90;
- exists型子查詢
把外層查詢結(jié)果拿到內(nèi)層,看內(nèi)層的查詢是否成立
例如:查詢哪些欄目下有商品,欄目表category,商品表goods
select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);
聯(lián)接
定義 A INNER/LEFT/RIGHT JOIN B操作中,A表被稱為左表,B表被稱為右表。
a) 內(nèi)關(guān)聯(lián): Inner Join on 作用:僅對(duì)滿足連接條件的列進(jìn)行關(guān)聯(lián),其中inner可省略
b) 左外連接:Left Outer Jion on 作用:其中outer可以省略。如A LEFT JOIN B,會(huì)輸出左表A中所有的數(shù)據(jù),同時(shí)將符合ON條件的右表B中搜索出來(lái)的結(jié)果合并到左表A表中,如果A表中存在而在B表中不存在,則結(jié)果集中會(huì)將查詢的B表字段值(如此處的P.PUNISHMENT字段)設(shè)置為NULL。 所以,LEFT JOIN的作用是: LEFT JOIN:從右表B中將符合ON條件的結(jié)果查詢出來(lái),合并到A表中,再作為一個(gè)結(jié)果集輸出。
c) 右外連接:Right Outer Jion on 作用:其中outer可以省略,而RIGHT JOIN剛好相反,“A RIGHT JOIN B ON ……”是將符合ON條件的A表搜索結(jié)果合并到B表中,作為一個(gè)結(jié)果集輸出:
練習(xí):
以前一篇MariaDB基礎(chǔ)中的練習(xí)題創(chuàng)建的數(shù)據(jù)庫(kù)為基礎(chǔ)



習(xí)題如下:
1.查詢所有學(xué)生信息:
select * from tbstudent;
2.查詢所有課程名稱及學(xué)分
select cosname,sum(coscredit) from tbcourse join tbsc on tbcourse.cosid=tbsc.cid group by(tbcourse.cosname);
3.查詢所有女學(xué)生的姓名和出生日期(篩選)
select stuname,stubirth from tbstudent where stusex=0;
4.查詢所有80后學(xué)生的姓名、性別和出生日期(篩選)
select stuname,stusex,stubirth from tbstudent where stubirth between '1980-1-1' and '1990-1-1';
5.查詢姓王的學(xué)生姓名和性別(模糊)
select stuname,stusex from tbstudent where stuname like '王%';
6.查詢姓郭名字總共兩個(gè)字的學(xué)生的姓名(模糊)
select stuname from tbstudent where stuname like '郭_';
7.查詢姓郭名字總共三個(gè)字的學(xué)生的姓名(模糊)
select stuname from tbstudent where stuname like '郭_';
8.查詢名字中有王字的的學(xué)生的姓名(模糊)
select stuname from tbstudent where stuname like '%王%';
9.查詢沒有錄入家庭住址和照片的學(xué)生姓名(多條件篩選和空值處理)
select stuname from tbstudent where stuaddr is null and stuphoto is null;
10.查詢學(xué)生選課的所有日期(去重)
select distinct(scdate) from tbsc;
11.查詢學(xué)生的姓名和生日按年齡從大到小排列(排序)
select stuname,stubirth from tbstudent order by(stubirth);
12.查詢所有錄入了家庭住址的男學(xué)生的姓名、出生日期和家庭住址按年齡從小到大排列(多條件篩選和排序)
select stuname,stubirth,stuaddr from tbstudent where stuaddr is not null and stusex=1 order by(stubirth);
13.查詢年齡最大的學(xué)生的出生日期(聚合函數(shù))
select stuname,stubirth from tbstudent where stubirth=(select min(stubirth) from tbstudent);
14.查詢年齡最小的學(xué)生的出生日期(聚合函數(shù))
select stuname,stubirth from tbstudent where stubirth=(select max(stubirth) from tbstudent);
15.查詢男女學(xué)生的人數(shù)(分組和聚合函數(shù))
select stusex,count(stusex) from tbstudent group by(stusex);
16.查詢課程編號(hào)為1111的課程的平均成績(jī)(篩選和聚合函數(shù))
select cid,ifnull(avg(score), 0) from tbsc group by(cid);
17.查詢學(xué)號(hào)為1001的學(xué)生的所有課程的總成績(jī)(篩選和聚合函數(shù))
select sid,sum(score) from tbsc where sid=1001;
18.查詢每個(gè)學(xué)生的學(xué)號(hào)和平均成績(jī),null值處理為0(分組和聚合函數(shù))
select stuid,ifnull(c.avg,0) from tbstudent t left join (select sid,avg(tbsc.score) as avg from tbsc group by(sid)) as c on c.sid=t.stuid;
19.查詢平均成績(jī)大于等于90分的學(xué)生的學(xué)號(hào)和平均成績(jī)
select ts1.stuid,t2.avg from tbstudent ts1,(select sid,avg(score) as avg from tbsc group by(sid)) as t2 where t2.sid=ts1.stuid and t2.avg>=90;
或者
select sid,avg(score) as avg from tbsc group by(sid) having avg(score)>=90;
20.查詢年齡最大的學(xué)生的姓名
select t1.stuname from tbstudent t1 where t1.stubirth=(select min(stubirth) from tbstudent);
21.查詢選了兩門以上的課程的學(xué)生姓名
select t1.stuname,t1.stuid from tbstudent t1 join (select sid,count(cid) as count from tbsc group by(sid)) as t on t.sid=t1.stuid and t.count>=2;
22.查詢選課學(xué)生的姓名和 平均成績(jī)
select t1.stuname,ifnull(t2.avg,0) from tbstudent t1 join (select sid,avg(score) as avg from tbsc group by(sid)) as t2 on t1.stuid=t2.sid;
23.查詢學(xué)生姓名、所選課程名稱和成績(jī)
select t1.stuname,t2.cosname,ifnull(t3.score,0) from tbstudent t1 join tbsc t3 on t1.stuid=t3.sid join tbcourse t2 on t2.cosid=t3.cid;
24.查詢每個(gè)學(xué)生的姓名和選課數(shù)量
select t1.stuname,ifnull(t2.count,0) from tbstudent t1 left join (select sid,count(cid) as count from tbsc group by(sid)) as t2 on t1.stuid=t2.sid;