day 05 多表連接查詢(xún)
1.select 多表
1.1 介紹
內(nèi)連接 inner join
外連接 left join right join
笛卡爾
1.2 作用
聚合多張表數(shù)據(jù),實(shí)現(xiàn)查詢(xún)需求
查詢(xún)?nèi)丝跀?shù)小于100人城市名,國(guó)家名,國(guó)土面積?
1.3 多表連接的語(yǔ)法
1.3.1 內(nèi)連接(交集)
FROM A
INNER JOIN B
ON A.x=B.y
1.3.2 外連接
FROM A
LEFT JOIN B
ON A.x=B.y
FROM A
RIGHT JOIN B
ON A.x=B.y
1.3.3 笛卡爾乘積
FROM A
JOIN B
1.4 多表連接例子
-- 1. 查詢(xún)?nèi)丝跀?shù)小于100人城市名,國(guó)家名,國(guó)土面積?
-- 套路 : 1. 找關(guān)聯(lián)表 2. 找關(guān)系列
USE world
SHOW TABLES;
DESC city;
DESC country;
SELECT
city.name,
country.name,
country.SurfaceArea,
city.`Population`
FROM city
JOIN country
ON city.`CountryCode`=country.`Code`
WHERE city.`Population`<100
-- 2. 統(tǒng)計(jì)查詢(xún)每位學(xué)員的平均分
SELECT student.xid ,
student.xname,
AVG(score.score)
FROM student
JOIN score
ON student.xid=score.xid
GROUP BY student.xid,student.xname
-- 3. 統(tǒng)計(jì)每位學(xué)員學(xué)習(xí)了幾門(mén)課
SELECT student.xid ,
student.xname,
COUNT(score.score)
FROM student
JOIN score
ON student.xid=score.xid
GROUP BY student.xid,student.xname
-- 4. 查詢(xún)每位老師教的課程名和價(jià)格
SELECT teacher.tname,course.`cname`,course.`cprice`
FROM teacher
JOIN course
ON teacher.`tid`=course.`tid`;
-- 5. 每位老師教的學(xué)生的個(gè)數(shù)和姓名列表
SELECT
teacher.tname,
COUNT(student.xid),
GROUP_CONCAT(student.xname)
FROM student
JOIN score
ON student.xid=score.xid
JOIN course
ON score.cid=course.cid
JOIN teacher
ON course.tid=teacher.tid
GROUP BY teacher.tid,teacher.tname;
實(shí)例:
1. 查詢(xún)oldguo老師教的學(xué)生名
SELECT CONCAT(teacher.`tname`,"_",teacher.`tid`) AS "教師名",
GROUP_CONCAT(student.`xname`) AS "學(xué)生名"
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
JOIN student
ON score.`xid`=student.`xid`
WHERE teacher.`tname`='oldguo'
GROUP BY teacher.`tname`,teacher.`tid`
2.查詢(xún)oldguo所教課程的平均分?jǐn)?shù).
SELECT CONCAT(teacher.`tname`,"_",teacher.`tid`) AS "教師名",
course.`cname`,
AVG(score.`score`)
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
WHERE teacher.`tname`='oldguo'
GROUP BY teacher.`tname`,teacher.`tid`,course.`cname`
3.每位老師所教課程的平均分,并按平均分排序.
SELECT CONCAT(teacher.`tname`,"_",teacher.`tid`),
course.`cname`,AVG(score.`score`)
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
GROUP BY teacher.`tname`,teacher.`tid`,course.`cname`
ORDER BY score.`score` DESC
4.查詢(xún)白龍馬,學(xué)習(xí)的課程名稱(chēng)有哪些.
SELECT CONCAT(student.`xname`,"_",student.`xid`),
GROUP_CONCAT(course.`cname`)
FROM student JOIN score
ON student.`xid`=score.`xid`
JOIN course
ON score.`cid`=course.`cid`
WHERE student.`xname`='白龍馬'
GROUP BY student.`xname`,student.`xid`
5. 統(tǒng)計(jì)每位老師,每門(mén)課程,分別的收入情況
SELECT teacher.`tname`,course.`cname`,course.`cprice`*COUNT(score.`xid`)
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
GROUP BY teacher.`tname`,teacher.`tid`,course.`cid`
6.統(tǒng)計(jì)每門(mén)課程學(xué)習(xí)的人數(shù).
SELECT course.`cname`,COUNT(score.`xid`)
FROM course JOIN score
ON course.`cid`=score.`cid`
GROUP BY course.`cname`
7.查詢(xún)oldboy老師教的學(xué)生不及格的學(xué)生名單.
SELECT teacher.`tname`,GROUP_CONCAT(CONCAT(student.`xname`,":",score.`score`))
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
JOIN student
ON score.`xid`=student.`xid`
WHERE teacher.`tname`='oldboy' AND score.`score`<60
GROUP BY teacher.`tname`,teacher.`tid`
8.統(tǒng)計(jì)每位老師不及格學(xué)生名單.
SELECT teacher.`tname`,GROUP_CONCAT(CONCAT(student.`xname`,":",score.`score`))
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
JOIN student
ON score.`xid`=student.`xid`
WHERE score.`score`<60
GROUP BY teacher.`tname`,teacher.`tid`
9.每位老師所教課程的平均分,并按平均分排序.
SELECT teacher.`tname`,course.`cname`,
AVG(score.`score`)
FROM teacher JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
GROUP BY teacher.`tname`,teacher.`tid`,course.`cid`
ORDER BY AVG(score.`score`) DESC
10.查詢(xún)各科成績(jī)最高和最低的分:以如下形式顯示:課程ID蒜鸡,最高分腰吟,最低分.
SELECT course.`cid`,MAX(score.`score`),MIN(score.`score`)
FROM score JOIN course
ON score.`cid`=course.`cid`
GROUP BY course.`cid`
11.查詢(xún)平均成績(jī)大于60分的同學(xué)的學(xué)號(hào)和平均成績(jī).
SELECT CONCAT(student.`xname`,"_",student.`xid`),AVG(score.`score`)
FROM score
JOIN student
ON score.`xid`=student.`xid`
GROUP BY student.`xname`,student.`xid`
HAVING AVG(score.`score`)>60
或
SELECT
CONCAT(student.`xname`,"_",student.`xid`),
AVG(score.`score`)
FROM student
JOIN score
ON student.`xid`=score.`xid`
GROUP BY student.`xname`,student.`xid`
HAVING AVG(score.`score`)>60
ORDER BY AVG(score.`score`) DESC ;
12.統(tǒng)計(jì)各位老師,所教課程的及格率
SELECT teacher.`tname`,course.`cname`,
CONCAT(COUNT(CASE WHEN score.`score`>60 THEN 1 END )/COUNT(score.`xid`)*100,"%")
FROM teacher
JOIN course
ON teacher.`tid`=course.`tid`
JOIN score
ON course.`cid`=score.`cid`
GROUP BY teacher.`tid`,teacher.`tname`,course.`cid`
13.統(tǒng)計(jì)每門(mén)課程:優(yōu)秀(85分以上),良好(70-85),一般(60-70),不及格(小于60)的學(xué)生列表
SELECT course.`cname`,
GROUP_CONCAT(CASE WHEN score.`score`>=85 THEN student.`xname` END ) AS '優(yōu)秀',
GROUP_CONCAT(CASE WHEN score.`score`>=70 AND score.`score`<85 THEN student.`xname` END ) AS '良好',
GROUP_CONCAT(CASE WHEN score.`score`>=60 AND score.`score`<70 THEN student.`xname` END ) AS '一般',
GROUP_CONCAT(CASE WHEN score.`score`<60 THEN student.`xname` END ) AS '不及格'
FROM student
JOIN score
ON student.xid = score.xid
JOIN course
ON score.`cid`=course.`cid`
GROUP BY course.`c
1.5 left/right join 外連接應(yīng)用
inner join 內(nèi)連接應(yīng)用
一般應(yīng)用在強(qiáng)制驅(qū)動(dòng)表時(shí),強(qiáng)制小結(jié)果集驅(qū)動(dòng)大表
---生產(chǎn)總可以使用left join 強(qiáng)制驅(qū)動(dòng)表 ,盡量減少next loop 的出現(xiàn)
---為啥要強(qiáng)制节视?
inner join 優(yōu)化器 自動(dòng)選擇,按照索引選擇的幾率較大鳄橘,導(dǎo)致效率比較低折柠。通過(guò)大量的讀寫(xiě)磁盤(pán),導(dǎo)致磁盤(pán)的損壞带膜。
1.6.1 列別名
SELECT
course.`cname` AS 課程名稱(chēng),
GROUP_CONCAT(CASE WHEN score.`score` >= 85 THEN student.xname END ) AS "優(yōu)秀",
GROUP_CONCAT(CASE WHEN score.`score` >=70 AND score.`score` < 85 THEN student.xname END) AS "良好",
GROUP_CONCAT(CASE WHEN score.`score` >=60 AND score.`score` <70 THEN student.xname END )AS "一般",
GROUP_CONCAT(CASE WHEN score.`score` <60 THEN student.xname END ) AS "不及格"
FROM student
JOIN score
ON student.xid = score.xid
JOIN course
ON score.`cid`=course.`cid`
GROUP BY course.`cid`;
1.6.2 表別名
SELECT
CONCAT(te.tname,"_",te.tid) AS "教師名"
,GROUP_CONCAT(CONCAT(st.xname,":",sc.score))
FROM teacher AS te
JOIN course AS co
ON te.`tid`=co.`tid`
JOIN score AS sc
ON co.`cid`=sc.`cid`
JOIN student AS st
ON sc.`xid`=st.`xid`
WHERE sc.`score`<60
GROUP BY te.tid,te.tname;
2.show 語(yǔ)句的列表介紹
----查看所有的庫(kù)
show databases;
--查看當(dāng)前庫(kù)下的所有表
show tables;
show tables from world;
--查看當(dāng)前并發(fā)會(huì)話信息
show processlist;
--查看數(shù)據(jù)庫(kù)支持的權(quán)限
show privileges;
--查看數(shù)據(jù)庫(kù)參數(shù)信息
show variables;
show variables like '%trx%';
--查看字符集&效對(duì)規(guī)則
show charset; #查看字符集
show collation; #查看效對(duì)信息
---查看建庫(kù)&建表信息
mysql> show create database world;
mysql> show create table world.city;
-- 查看用戶權(quán)限
mysql> show grants for oldguo@'10.0.0.%';
-- 查看支持的存儲(chǔ)引擎
mysql> show engines;
-- 查詢(xún)表中索引信息
mysql> show index from world.city;
-- 查看數(shù)據(jù)庫(kù)當(dāng)前狀態(tài)信息
show status;
show status like '%lock%';
-- 查看InnoDB引擎相關(guān)的狀態(tài)信息(內(nèi)存,事務(wù),鎖,線程...)
show engine innodb status\G
-- 查看二進(jìn)制日志相關(guān)信息
show binary logs ;
show master status;
show binlog events in 'xxxx';
-- 查看主從復(fù)制相關(guān)信息
show relaylog events in 'xxxx';
show slave status \G
mysql> help show ;
3. information_schema 統(tǒng)計(jì)信息庫(kù)
3.1 介紹
視圖 ?
安全 : 只允許查詢(xún),不知道操作的是什么對(duì)象.
-
方便 : 只需要簡(jiǎn)單的select語(yǔ)句即可使用
create view a as SELECT CONCAT(te.tname,"_",te.tid) AS "教師名" ,GROUP_CONCAT(CONCAT(st.xname,":",sc.score)) FROM teacher AS te JOIN course AS co ON te.`tid`=co.`tid` JOIN score AS sc ON co.`cid`=sc.`cid` JOIN student AS st ON sc.`xid`=st.`xid` WHERE sc.`score`<60 GROUP BY te.tid,te.tname; select * from a;
3.2 作用:
1.方便做數(shù)據(jù)庫(kù)資產(chǎn)統(tǒng)計(jì)
資產(chǎn)就包括: 庫(kù) 表
庫(kù)\表 :
個(gè)數(shù)
數(shù)據(jù)量(大小,行數(shù))
每張表的數(shù)據(jù)字典信息
2.獲取到server 層狀態(tài)信息
3.獲取到innodb 引擎層的狀態(tài)信息
3.3 應(yīng)用舉例
tables
table_schema: 表所在的庫(kù)
table_name: 表名
engine : 表的引擎
table_rows: 表的行數(shù)
AVG_ROW_LENGTH: 平均行長(zhǎng)度(字節(jié))
INDEX_LENGTH: 索引占用的長(zhǎng)度(字節(jié))
TABLE_COMMENT:表注釋
---統(tǒng)計(jì)信息庫(kù)information_schema
例:
1. 簡(jiǎn)單查詢(xún)體驗(yàn)TABLES信息
SELECT * FROM TABLES;
2. 所有業(yè)務(wù)庫(kù)和表的名字.
SELECT table_schema,table_name
FROM information_schema.tables
WHERE table_schema NOT IN('sys','information_schema','performance_schema','mysql');
3 .統(tǒng)計(jì)每個(gè)業(yè)務(wù)庫(kù),表的個(gè)數(shù)和列表
SELECT TABLE_SCHEMA,COUNT(TABLE_NAME),GROUP_CONCAT(TABLE_NAME)
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql')
GROUP BY TABLE_SCHEMA
4. 統(tǒng)計(jì)業(yè)務(wù)數(shù)據(jù)庫(kù)的總數(shù)據(jù)量
SELECT SUM(TABLE_rows * AVG_ROW_LENGTH+INDEX_LENGTH)/1024 AS total_kb
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql');
5.每個(gè)業(yè)務(wù)庫(kù)分別統(tǒng)計(jì)數(shù)據(jù)量
SELECT TABLE_SCHEMA,SUM(TABLE_ROWS * AVG_ROW_LENGTH+INDEX_LENGTH)/1024 AS TOTAL_kb
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql')
GROUP BY TABLE_SCHEMA
ORDER BY total_kb DESC;
6.top 3 數(shù)據(jù)量大的表
SELECT TABLE_SCHEMA,TABLE_NAME,(TABLE_ROWS * AVG_ROW_LENGTH+INDEX_LENGTH)/1024 AS table_kb
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql')
ORDER BY table_kb DESC
LIMIT 3
7.查詢(xún)所有非INNODB的表
SELECT TABLE_SCHEMA,TABLE_NAME,ENGINE
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql')
AND ENGINE <> 'innodb'
8.查詢(xún)所有非INNODB的表 , 并且提出修改建議
SELECT TABLE_SCHEMA,TABLE_NAME,ENGINE,CONCAT("alter table",table_schema,".",table_name,"engine=innodb;") AS "修改建議"
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql')
AND ENGINE <> 'innodb'
9.所有業(yè)務(wù)庫(kù)和表的名字,并且生成備份語(yǔ)句
SELECT TABLE_SCHEMA,TABLE_NAME,
CONCAT("mysqldump", TABLE_SCHEMA,".",table_name, "> /bak/",table_schema,"_",table_name,".sql") AS "備份"
FROM information_schema.`TABLES`
WHERE TABLE_SCHEMA NOT IN ('sys','information_schema','performance_schema','mysql')