SELECT from WORLD Tutorial
1 | -- 1 |
SELECT from Nobel Tutorial
1 | --1 |
SELECT within SELECT Tutorial
1 | --1 |
Nested SELECT Quiz
1 | --1 |
- 4
1
2
3
4
5
6
7
8--5
SELECT name FROM bbc
WHERE gdp > (SELECT MAX(gdp) FROM bbc WHERE region = 'Africa')
--6
SELECT name FROM bbc
WHERE population < (SELECT population FROM bbc WHERE name='Russia')
AND population > (SELECT population FROM bbc WHERE name='Denmark')
- 7
SUM and COUNT
1 | --1 |
SUM and COUNT QUIZ
1 | --1 |
- 8
The JOIN operation
JOIN 的说明
1
2SELECT *
FROM game JOIN goal ON (id=matchid)- 语句 FROM 表示合并 game 和 goal 两个表的数据。
- ON 表示如何找出 game 中每一列应该配对 goal 中的哪一列 (game 的 id 需要配对 goal 的matchid),就是ON (game.id=goal.matchid)
- GROUP BY 的说明 (参考 MySQL 必知必会 P84)
- 第11题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70--1
SELECT matchid,player
FROM goal
WHERE teamid='GER';
--2
SELECT id,stadium,team1,team2
FROM game
WHERE id=1012;
--3
SELECT player,teamid,stadium,mdate
FROM game JOIN goal ON (id=matchid)
WHERE teamid='GER';
--4
SELECT team1,team2,player
FROM game JOIN goal ON (id=matchid)
WHERE player LIKE 'Mario%';
--5
SELECT player, teamid, coach, gtime
FROM goal JOIN eteam ON (teamid=id)
WHERE gtime<=10;
--6
SELECT mdate,teamname
FROM game JOIN eteam ON (team1=eteam.id)
WHERE coach='Fernando Santos'
GROUP BY mdate, teamname;
--7
SELECT player
FROM game JOIN goal ON (id=matchid)
WHERE stadium = 'National Stadium, Warsaw';
--8 注意 player 是不重复的
SELECT DISTINCT player
FROM game JOIN goal ON (id=matchid)
WHERE (team1='GER' OR team2='GER') AND teamid<>'GER';
--9
SELECT teamname, COUNT(player)
FROM eteam JOIN goal ON (id=teamid)
GROUP BY teamname;
--10
SELECT stadium,COUNT(teamid)
FROM game JOIN goal ON id=matchid
GROUP BY stadium;
--11 这里要以 matchid,mdate 为分组,少一个都不行
SELECT matchid, mdate, COUNT(*)
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid,mdate;
--12
SELECT matchid, mdate, COUNT(gtime)
FROM goal JOIN game ON matchid=id
WHERE teamid='GER'
GROUP BY matchid,mdate;
--13 CASE WHEN THEN ELSE END
SELECT mdate,team1,
SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) AS score1,
team2,
SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) AS score2
FROM game LEFT JOIN goal ON matchid = id
GROUP BY mdate,matchid,team1,team2;
第 13 题,如果不用 SUM 统计 CASE 总和的化,得到的列中行值不是 0 就是 1;另外,需要 LEFT JOIN,因为仅仅 JOIN 的话,如果有某行全为 0(没有进球),则该行就不会显示了。LEFT JOIN 无论如何都会显示,不进球就全给 0 值(或NULL)
JOIN Quiz
1
2
3
4
5
6
7
8
9
10
11
12--1
game JOIN goal ON (id=matchid)
--2
matchid, teamid, player, gtime, id, teamname, coach
--3
SELECT player, teamid, COUNT(*)
FROM game JOIN goal ON matchid = id
WHERE (team1 = "GRE" OR team2 = "GRE")
AND teamid != 'GRE'
GROUP BY player, teamid4
1
2
3
4
5
6
7
8
9
10
11
12--5
SELECT DISTINCT player, teamid
FROM game JOIN goal ON matchid = id
WHERE stadium = 'National Stadium, Warsaw'
AND (team1 = 'POL' OR team2 = 'POL')
AND teamid != 'POL'
--6
SELECT DISTINCT player, teamid, gtime
FROM game JOIN goal ON matchid = id
WHERE stadium = 'Stadion Miejski (Wroclaw)'
AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
- 7
Using Null
- 第四题,JOIN 前后换序,结果是不一样的 参考
- LEFT JOIN 先匹配左表
- RIGHT JOIN 先匹配右表
第五题,COALESCE 说明
COALESCE(x,y,z)
返回括号内参数的第一个非空值1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53--1
SELECT name
FROM teacher
WHERE dept is NULL;
--2
SELECT teacher.name, dept.name
FROM teacher JOIN dept
ON (teacher.dept=dept.id)
--3
SELECT teacher.name, dept.name
FROM teacher LEFT JOIN dept
ON (teacher.dept=dept.id);
--4
SELECT teacher.name,dept.name
FROM dept LEFT JOIN teacher
ON (teacher.dept=dept.id);
或者
SELECT teacher.name,dept.name
FROM teacher RIGHT JOIN dept
ON (teacher.dept=dept.id);
--5 COALESCE
SELECT name,COALESCE(mobile,'07986 444 2266')
FROM teacher;
--6
SELECT teacher.name,COALESCE(dept.name,'None')
FROM teacher LEFT JOIN dept
ON teacher.dept=dept.id;
--7
SELECT COUNT(name), COUNT(mobile)
FROM teacher;
--8
SELECT dept.name, COUNT(teacher.name)
FROM teacher RIGHT JOIN dept ON teacher.dept=dept.id
GROUP BY dept.name;
--9
SELECT name,CASE WHEN dept IN (1,2) THEN 'Sci'
ELSE 'Art' END
FROM teacher;
--10
SELECT name,
CASE WHEN dept IN (1,2) THEN 'Sci'
WHEN dept=3 THEN 'Art'
ELSE 'None' END
FROM teacher;Using Null Quiz
1
2
3
4
5
6
7
8
9
10
11
12
13
14--1
SELECT teacher.name, dept.name FROM teacher LEFT OUTER JOIN dept ON (teacher.dept = dept.id)
--2
SELECT dept.name FROM teacher JOIN dept ON (dept.id = teacher.dept) WHERE teacher.name = 'Cutflower'
--3
SELECT dept.name, COUNT(teacher.name) FROM teacher RIGHT JOIN dept ON dept.id = teacher.dept GROUP BY dept.name
--4
display 0 in result column for all teachers without department
--5
'four' for Throd
6
Self join
- MySQL 必知必会 P107
自联结:相当于一张表当两张用 参考
- 帮助理解的 Leetcode 题 181. 超过经理收入的员工
1
2
3
4# Write your MySQL query statement below
SELECT a.Name AS Employee
FROM Employee a JOIN Employee b ON a.ManagerId=b.Id
WHERE a.Salary > b.Salary
- 帮助理解的 Leetcode 题 181. 超过经理收入的员工
需回顾 6-10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72--1
SELECT COUNT(*)
FROM stops;
--2
SELECT id
FROM stops
WHERE name='Craiglockhart';
--3
SELECT id,name
FROM stops JOIN route ON id=stop
WHERE num='4' AND company='LRT';
--4
SELECT company, num, COUNT(*)
FROM route WHERE stop=149 OR stop=53
GROUP BY company, num
HAVING COUNT(*)=2;
--5
SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
WHERE a.stop=53 AND b.stop=149;
--6
SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart' AND stopb.name='London Road';
--7
SELECT DISTINCT a.company,a.num
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
WHERE a.stop=115 AND b.stop=137;
--8 修改 6 的代码
SELECT a.company, a.num
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON a.stop=stopa.id
JOIN stops stopb ON b.stop=stopb.id
WHERE stopa.name='Craiglockhart'and stopb.name ='Tollcross';
--9
select d.name,a.company,a.num
from route a join route b
on a.num =b.num and a.company = b.company
join stops c on (a.stop = c.id)
join stops d on (b.stop = d.id)
where c.name ='Craiglockhart';
--10
select distinct x.num,x.company,name,y.num,y.company
from(select a.num,a.company,b.stop
from route a
join route b
on a.num = b.num and a.company = b.company
and a.stop != b.stop
where a.stop = (select id from stops where name ='Craiglockhart')) as x
join (select c.num,c.company,c.stop
from route c
join route d
on c.num = d.num and c.company = d.company
and c.stop != d.stop
where d.stop =(select id from stops where name = 'Lochend'))as y
on x.stop = y.stop
join stops on x.stop = stops.id
order by x.num,stops.name,y.num