我在三个表之间使用连接查询。我的查询并未完全显示我想要的结果。有三个表team
,school
和game
。例如,如何显示劳伦斯北高中(school_id = 11111
)的总胜数?
当前查询:
SELECT school.school_name
FROM school
INNER JOIN team ON school.school_id = team.school_id
INNER JOIN game ON team.team_id = game.game_id
WHERE school.school_id = 11111
使用的表:
SQL> Select * From Team;
TEAM_ID SPORT_ID SCHOOL_ID TEAM_NAME
---------- ---------- ---------- ----------
1 1 11111 WildCats
2 2 11111 WILDCATS
3 3 11111 WildCats
4 4 11111 WILDCATS
5 5 11111 WildCats
6 6 11111 WildCats
7 7 11111 WildCats
8 1 123134 Bears
9 2 123134 Bears
10 3 123134 BEARS
11 4 123134 Bears
12 5 123134 Bears
13 6 123134 BEars
SQL> Select * From School;
SCHOOL_ID SCHOOL_NAME SCHOOL_TYPE
---------- ----------------------------- ------------
11546 Ivy Tech College COLLegE
11090 LAWRENCE Central Grade School GRADE SCHOOL
11111 Lawrence NORTH High School HIGH SCHooL
19283 Howe High SCHOOL High SchooL
123134 Lawrence Central High School HIGH SCHOOL
192 Little Big Horn Grade School GRADE SCHOOL
SQL> Select * From Game;
TEAM_ID GAME_DATE R
---------- --------- -
1 10-SEP-98 w
1 12-SEP-98 W
2 17-SEP-98 w
2 18-SEP-98 W
3 18-SEP-98 w
3 11-SEP-98 W
4 20-SEP-98 L
4 21-SEP-98 l
5 21-SEP-98 L
5 19-SEP-98 l
6 26-SEP-98 L
6 27-SEP-98 W
7 28-SEP-98 w
7 29-SEP-98 W
8 11-OCT-98 W
8 12-OCT-98 w
9 13-OCT-98 L
9 14-OCT-98 L
10 14-OCT-98 L
11 15-OCT-98 L
12 16-OCT-98 W
13 17-OCT-98 W
11 18-OCT-98 W
12 19-OCT-98 W
13 11-NOV-98
11 12-NOV-98
答案 0 :(得分:3)
试试这个
SELECT count(*) as Total_wins FROM school INNER JOIN team ON school.school_id = team.school_id INNER JOIN game ON team.team_id = game.game_id WHERE school.school_id = 11111 and game.r = 'W'
答案 1 :(得分:1)
您可以在COUNT
上使用Where
Group By
条与schoolname
一起使用。
SELECT school.school_name, COUNT(game.R) as TotalWins
FROM school INNER JOIN team
ON school.school_id = team.school_id
INNER JOIN game
ON team.team_id = game.game_id
WHERE
school.school_id = 11111
AND game.R = 'W'
如果你是为一所学校做的,你可以删除Group By
,但如果你想获得所有学校的计数,你需要应用Group By
并删除Chool_id
从Where
子句过滤。
答案 2 :(得分:0)
您还必须在where子句中指定运动ID。
where school.school_id = 11111 and
team.sportid = (what ever the sport id you want to see)