一 匯總和分組數(shù)據(jù)
查詢(xún)語(yǔ)句 —> 結(jié)果集(多條數(shù)據(jù)) —> 聚合函數(shù) ?—-> 單行記錄
1.常用的聚合函數(shù):
sum() ? ? ? ? 數(shù)字 ? ? ? ? ? ? ? ? ? ? ? ? ?對(duì)指定列中的所有非空值求總和
avg() ? ? ? ? ?數(shù)字 ? ? ? ? ? ? ? ? ? ? ? ? ?對(duì)指定列中的所有非空值求平均值
min() ? ?數(shù)字,字符,datetime ? ? ? ?返回指定列中的最小數(shù)字,最早的日期或者最小的字符串
max() ? 數(shù)字,字符,datetime ? ? ? ?返回指定列中的最大數(shù)字,最近的日期或者最大的字符集
count() ? 任意基于行的數(shù)據(jù)類(lèi)型 ? ? ? 統(tǒng)計(jì)結(jié)果集合眾全部記錄行的數(shù)量
?
例:查詢(xún)玩家表中一共有多少名玩家信息
select count (user_qq) from users
select count(*) from users
例:查詢(xún)QQ號(hào)是12301的玩家游戲的總分?jǐn)?shù)
select sum(score) as ‘總分?jǐn)?shù)’ from scores where user_qq=’12301′
例:查詢(xún)QQ號(hào)是12302玩家的評(píng)價(jià)分?jǐn)?shù)
select avg(score) as ‘平均分?jǐn)?shù)’ from scores where user_qq=’12302’
例:查詢(xún)游戲編號(hào)的1的最高分?jǐn)?shù)
select max(score) as ‘最高分?jǐn)?shù)’ from score where gno=1
例: 查詢(xún)QQ號(hào)是12302的玩家的總分?jǐn)?shù),平均分?jǐn)?shù)和最高分?jǐn)?shù)
select sum(score) as ‘總分’,avg(score) as ‘平均分’,max(score) as ‘最高分’ from scores where user_qq =’12302′
?
2. 使用GROUP BY分組
例:查詢(xún)每個(gè)玩家的總分?jǐn)?shù),平均分?jǐn)?shù),最高分?jǐn)?shù)
select sum(score) as ‘總分’,avg(score) as ‘平均分’,max(score) as ‘最高分’ from scores group by user_qq
例: 查詢(xún)每個(gè)玩家的平均分?jǐn)?shù),并顯示玩家QQ號(hào)和平均分?jǐn)?shù)
select user_qq, avg(score) as ‘平均分?jǐn)?shù)’ from scores group by user_qq
?
3. 篩選分組結(jié)果
? 在使用GROUP BY子句時(shí),可用HAVING子句為分組統(tǒng)計(jì)進(jìn)一步設(shè)置統(tǒng)計(jì)條件,HAVING子句與GROUP BY 子句的關(guān)系相當(dāng)于WHERE子句與SELECT子句之間的關(guān)系
?與WHERE子句的區(qū)別是,在HAVING子句中是以聚合函數(shù)的統(tǒng)計(jì)結(jié)果為篩選條件。
?例:查詢(xún)平均分?jǐn)?shù)大于4000的玩家QQ號(hào),總分?jǐn)?shù),平均分?jǐn)?shù)
select user_qq, sum(score) as’總分?jǐn)?shù)’, avg(score) as ‘平均分?jǐn)?shù)’ from scores group by user_qq having avg(score) > 4000
例:查詢(xún)所有用戶的平均分?jǐn)?shù),和總分?jǐn)?shù),并按平均分?jǐn)?shù)倒序排列
select user_qq,avg(score) as ‘平均分?jǐn)?shù)’ , sun(score) as ‘總分?jǐn)?shù)’ from scores group by user_qq orde by avg(score) desc
?
4.SELECT 語(yǔ)句的執(zhí)行順序
? ? ?from 子句指定數(shù)據(jù)源
? ? ?where 子句基于指定的條件對(duì)記錄進(jìn)行篩選
? ? ?group by 子句將數(shù)據(jù)劃分為多個(gè)分組
? ? ?使用聚合函數(shù)進(jìn)行計(jì)算
? ? ?使用having子句篩選分組
? ? ?使用order by 子句對(duì)結(jié)果集進(jìn)行排序
?
二 連接查詢(xún)
1. 多表連接
例:查詢(xún)分?jǐn)?shù)信息,顯示玩家昵稱(chēng),游戲名稱(chēng)和分?jǐn)?shù)
select user_name as ‘昵稱(chēng)’, game as ‘游戲名稱(chēng)’ , score as ‘分?jǐn)?shù)’ from users.user_qq = scores.user_qq and game.gno= scores.gno
連接查詢(xún)分為內(nèi)連接和外連接兩種
內(nèi)連接特點(diǎn):相連接的兩張表地位平等
? ? ? ? ? ? ? ? ?如果一張表中在另一張表中不存在對(duì)應(yīng)數(shù)據(jù),則不做連接
? ? ? ? ? ? ? ? ?from 子句后面直接出現(xiàn)多個(gè)表名,這種連接方式即屬于內(nèi)連接,是隱式內(nèi)連接
? ? ? ? ? ? ? ? ?顯示內(nèi)連接格式:select col_list from table1[inner] join table2 on table1.col=table2.clo1
? ? ? ? ? ? ? ? ?例:查詢(xún)分?jǐn)?shù)信息,顯示玩家昵稱(chēng),游戲名稱(chēng)和分?jǐn)?shù)
? ? ? ? ? ? ? ? ?select user_name as ‘昵稱(chēng)’, g_name as ‘游戲名稱(chēng)’, score as ‘分?jǐn)?shù)’ from games inner join scores on games.gno =scores.gno
? ? ? ? ? ? ? ? ?inner join users on score.user_qq=user.user_qq
? ? ? ? ? ? ? ? ?例:查詢(xún)每個(gè)玩家的昵稱(chēng),總分和平均分
? ? ? ? ? ? ? ? ? ? select user_name as ‘昵稱(chēng)’,sum(score) as ‘總分’,avg(score) as ‘平均分’ from users U inner join scores S on S.user_qq = U.user_qq group by ? ? ? ? ? ? ? ? ? ? ? ? U.user_qq,user_name
? ? ? ? ? ? ? ? ?例:查詢(xún)平均分?jǐn)?shù)大于3500的分?jǐn)?shù)信息,顯示玩家昵稱(chēng),總分?jǐn)?shù),平均分?jǐn)?shù),并按照平均分?jǐn)?shù)降序排列
? ? ? ? ? ? ? ? ??select user_name as ‘昵稱(chēng)’,sum(score) as ‘總分’,avg(score) as ‘平均分’ from users U inner join scores S on S.user_qq = U.user_qq group by ? ? ? ? ? ? ? ? ? ? ? ? U.user_qq,user_name having avg(score)>3500 order by avg(score) desc
外連接分為左連接和右外連接
外連接特點(diǎn):做連接的兩個(gè)表地位不平等,其中有一張的基礎(chǔ)表
? ? ? ? ? ? ? ? ?基礎(chǔ)表中的每條數(shù)據(jù)必須出現(xiàn),即使另一張表中沒(méi)有數(shù)據(jù)與之匹配,也要用NULL補(bǔ)齊
? ? ? ? ? ? ? ? ?左外連接時(shí)左表是基礎(chǔ)表,右表外連接時(shí)右表是基礎(chǔ)表
? ? ? ? ? ? ? ? ?語(yǔ)句中先出現(xiàn)的表為’左表’, 后出現(xiàn)的表為’右表’
外連接格式: SELECT COL_LIST FROM TABLE1 LEFT/RIGHT[OUTER] JOIN TABLE2 ON TABLE1.COL=TABLE2.COL
?例:查詢(xún)所有玩家關(guān)于5號(hào)游戲的分?jǐn)?shù)信息
? ?select user_name as’昵稱(chēng)’ gno as ‘游戲編號(hào)’, score as ‘分?jǐn)?shù)’ from users U left join scores S on U.user_qq=S.user_qq and S.gno=5