mysql五表查询_5、MySQL多表查询
1、union 和 union all
還記得高中課本上學到的 交集 和 并集 的概念嗎?
union 就是并集的概念,而下面即將要提到的join,就是交集的概念。區別:
union:合并時,會自動刪除重復行
union all:就是無腦拼接,不去重
select * from student1
union
select * from student2
2、笛卡爾積(交叉連接)
笛卡爾積是沒有連接條件返回的結果。
2行3列的數據,和3行2列的數據,進行笛卡爾積后,產生一個6×6的數據表。
這就是笛卡爾積。
一般不太建議使用笛卡爾積,因為非常好資源,比如2個100×100的表進行笛卡爾積,你會得到一個10000×10000的數據表。
3、各種 join
join就是高中數據中,交并集中的交集概念。
基本語法就是 A join B on A.key = B.key,這里 on 后面就是返回2個表的關聯條件;
此處多提一句,通過建立外鍵索引,并使用外鍵進行關聯,可以提升SQL的運行效率;
在多提一句,建立索引對查詢是有利的,但對于維護數據庫(增刪改)是有弊的。一張圖看懂SQL join 的邏輯:
join的邏輯上圖已經講的很清楚了,重點是多練習;4
該題的思路2(有點意思昂):
select 課程號,
sum(case when 成績>=60 then 1 else 0 end) as 及格人數,
sum(case when 成績 < 60 then 1 else 0 end) as 不及格人數
from score
group by 課程號;
該題的解法2:
select a.課程號,b.課程名稱,
sum(case when 成績 between 85 and 100 then 1 else 0 end) as '[100-85]',
sum(case when 成績 >=70 and 成績<85 then 1 else 0 end) as '[85-70]',
sum(case when 成績>=60 and 成績<70 then 1 else 0 end) as '[70-60]',
sum(case when 成績<60 then 1 else 0 end) as '[<60]'
from score a
right join course b
on a.課程號 = b.課程號
group by a.課程號,b.課程名稱;
練習題:原網頁的問題描述簡直一言難盡。。。
1. 在進球表(goal)中查找德國球隊(teamid = 'GER')進球的比賽編號(matchid),進球球員姓名(player)
SELECT matchid, player
FROM goal
WHERE teamid = 'GER';
2. 在比賽信息表(game)查找比賽編號1012的信息
SELECT id, stadium, team1, team2
FROM game
where id = 1012;
3. 查找德國隊進球球員姓名,球隊編號(在進球信息表goal), 比賽地點,比賽日期(在比賽信息表game)
SELECT b.player, b.teamid, a.stadium, a.mdate
FROM game a
join goal b ON a.id = b.matchid
where teamid = 'GER';
4. 查找姓名中以Mario開頭的進球球員,符合條件球員參加比賽的對戰雙方
select a.player, b.team1, b.team2
from goal a
join game b on a.matchid = b.id
where player like 'Mario%';
5. 查找進球球員的姓名、球隊編號、教練、多長時間進球。要求多長時間進球<=10分鐘
SELECT player, teamid, coach, gtime
FROM goal a
join eteam b on a.teamid = b.id
WHERE gtime <= 10;
6. 'Fernando Santos'作為教練的比賽日期,球隊編號有哪些?
select b.mdate, a.teamname
from eteam a
join game b on a.id = b.team1
where coach = 'Fernando Santos';
7. 在比賽地點'National Stadium, Warsaw'有哪些進球球員?
select b.player
from game a
join goal b on a.id = b.matchid
where stadium = 'National Stadium, Warsaw';
8.射入德國球門的球員姓名
select distinct b.player
from game as a inner join goal as b on a.id = b.matchid
where (b.teamid = a.team1 and a.team2 = 'GER') or (b.teamid = a.team2 and a.team1 = 'GER');
9. 查找出球隊名稱,和每個球隊進球人數
select b.teamname, count(teamid)
from goal a
join eteam b on a.teamid = b.id
group by teamid
order by teamname;
10. 查找出所有比賽地點,每個比賽地點的進球數
select a.stadium, count(b.player) '進球數'
from game a
left join goal b on a.id = b.matchid
group by a.stadium;
11. 查找出有波蘭球隊'POL'參加的比賽編號,比賽日期,對應這場比賽的進球數
select a.id, a.mdate, count(b.player) '進球數'
from game a
left join goal b on a.id = b.matchid
where team1 = 'POL' or team2 = 'POL'
group by a.id;
12. 對于德國隊'GER'得分的每場比賽,顯示比賽編號,比賽日期和'GER'得分的進球數
select a.id, a.mdate, count(b.player) '進球數'
from game a
left join goal b on a.id = b.matchid
where b.teamid = 'GER'
group by a.id
having count(b.player) > 0 ;
13. 查找出所有比賽的日期,每場比賽中對戰雙方各自的進球數(也就是team1進球數,team2進球數)
select a.mdate,
a.team1,
sum(case when a.team1 = b.teamid then 1 else 0 end) '得分1',
a.team2,
sum(case when a.team2 = b.teamid then 1 else 0 end) '得分2'
from game a
left join goal b on a.id = b.matchid
group by a.mdate, a.team1, a.team2
order by a.mdate asc, a.team1, a.team2;
總結
整體來說,多表連接查詢這部分,最重要的是:
一定要熟悉各個表之間的關系!
一定要熟悉各個表之間的關系!
一定要熟悉各個表之間的關系!
只要掌握join的邏輯,并且熟悉表之間的關系,就不會很難!
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的mysql五表查询_5、MySQL多表查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 内存引擎_MySQL常见的三
- 下一篇: linux mysql 知乎_在 Lin