SQLZOO练习5--join(表的连接)
game表:
| id | mdate | stadium | team1 | team2 |
|---|---|---|---|---|
| 1001 | 8 June 2012 | National Stadium, Warsaw | POL | GRE |
| 1002 | 8 June 2012 | Stadion Miejski (Wroclaw) | RUS | CZE |
| 1003 | 12 June 2012 | Stadion Miejski (Wroclaw) | GRE | CZE |
| 1004 | 12 June 2012 | National Stadium, Warsaw | POL | RUS |
| …… | ||||
goal表:
| matchid | teamid | player | gtime |
|---|---|---|---|
| 1001 | POL | Robert Lewandowski | 17 |
| 1001 | GRE | Dimitris Salpingidis | 51 |
| 1002 | RUS | Alan Dzagoev | 15 |
| 1002 | RUS | Roman Pavlyuchenko | 82 |
| …… | |||
team表:
| id | teamname | coach |
|---|---|---|
| POL | Poland | Franciszek Smuda |
| RUS | Russia | Dick Advocaat |
| CZE | Czech Republic | Michal Bilek |
| GRE | Greece | Fernando Santos |
| …… | ||
1、获取德国的比赛id和参赛人员。
The first example shows the goal scored by a player with the last name 'Bender'. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime
Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'
SELECT matchid,player
FROM goal
WHERE teamid = 'GER';
2、Show id, stadium, team1, team2 for just game 1012
From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.
Notice in the that the column matchid in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table.
Show id, stadium, team1, team2 for just game 1012
SELECT id,stadium,team1,team2
FROM game
where id=1012;
3、Modify it to show the player, teamid, stadium and mdate for every German goal.
You can combine the two steps into a single query with a JOIN.
SELECT *
FROM game JOIN goal ON (id=matchid)
The FROM clause says to merge data from the goal table with that from the game table. The ON says how to figure out which rows in game go with which rows in goal - the matchid from goal must match id from game. (If we wanted to be more clear/specific we could sayON (game.id=goal.matchid)
The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.
Modify it to show the player, teamid, stadium and mdate for every German goal.
SELECT goal.player,goal.teamid,game.stadium,game.mdate
from goal
left join
game
on goal.matchid=game.id
where goal.teamid='GER';
4、Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'
Use the same JOIN as in the previous question.
select game.team1,game.team2,goal.player
from game
left join
goal
on game.id=goal.matchid
where goal.player like'Mario%';
5、Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10
The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id
select goal.player,goal.teamid,eteam.coach,goal.gtime
from goal
join eteam
on goal.teamid=eteam.id
where goal.gtime<=10;
6、List the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.
To JOIN game with eteam you could use either
game JOIN eteam ON (team1=eteam.id) or game JOIN eteam ON (team2=eteam.id)
Notice that because id is a column name in both game and eteam you must specify eteam.id instead of just id
select game.mdate,eteam.teamname
from game
join
eteam
on game.team1=eteam.id
where eteam.coach='Fernando Santos';
7、List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'
select goal.player
from goal
join game
on goal.matchid=game.id
where game.stadium='National Stadium, Warsaw';
8、The example query shows all goals scored in the Germany-Greece quarterfinal.
Instead show the name of all players who scored a goal against Germany.
Select goals scored only by non-German players in matches where GER was the id of either team1 or team2.
You can use teamid!='GER' to prevent listing German players.
You can use DISTINCT to stop players being listed twice.
select DISTINCT(goal.player)
from goal
join
game
on goal.matchid=game.id
where teamid !='GER' and (team1='GER'or team2='GER');
9、Show teamname and the total number of goals scored.
select eteam.teamname,count(teamid)
from eteam
join goal
on eteam.id=goal.teamid
group by teamname;
10、Show the stadium and the number of goals scored in each stadium.
select game.stadium,count(teamid)
from game
join goal
on game.id=goal.matchid
group by stadium;
11、For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT goal.matchid,game.mdate, COUNT(player) from goal join game on goal.matchid=game.id where team1 LIKE '%POL%' or team2 LIKE '%POL%' group by matchid,mdate;
12、For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
select goal.matchid,game.mdate,count(player)
from goal
join game
on goal.matchid=game.id
where teamid LIKE'%GER%'
group by matchid,mdate;
13、List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.
SELECT game.mdate,game.team1,
sum(case when teamid=team1 then 1 else 0 end) as score1,
game.team2,
sum(case when teamid=team2 then 1 else 0 end) as score2
from game
left join goal
on game.id=goal.matchid
group by mdate,matchid,team1,team2;
解题思路:
️game表和goal表的连接
️case when,判断,来得出score1,score2。
SQLZOO练习5--join(表的连接)的更多相关文章
- Oracle 表的连接方式(2)-----HASH JOIN的基本机制2
Hash算法原理 对于什么是Hash算法原理?这个问题有点难度,不是很好说清楚,来做一个比喻吧:我们有很多的小猪,每个的体重都不一样,假设体重分布比较平均(我们考虑到公斤级别),我们按照体重来分,划分 ...
- oracle 表连接 - hash join 哈希连接
一. hash 连接(哈希连接)原理 指的是两个表连接时, 先利用两表中记录较少的表在内存中建立 hash 表, 然后扫描记录较多的表并探測 hash 表, 找出与 hash 表相匹配的行来得到结果集 ...
- SQL 经典回顾:JOIN 表连接操作不完全指南
2017-02-23 小峰 ITPUB 点击上方“蓝字”可以关注我们哦  |转载自:码农网 |原文链接:www.codeceo.com/article/sql-join-guide.html ...
- EF的表左连接方法Include和Join
在EF中表连接常用的有Join()和Include(),两者都可以实现两张表的连接,但又有所不同. 例如有个唱片表Album(AlbumId,Name,CreateDate,GenreId),表中含外 ...
- sql Left right join 多表 注意表的连接顺序
多表左/右连接,表的连接顺序也可以影响查询速度 左连接时,应该把小表放在前面连接例子:A.B.C三表左连接情况1:A先和B连接,得到100条记录100条记录再和C左连接情况2:A先和C连接,得到50条 ...
- 数据库中的左连接(left join)和右连接(right join)区别
Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...
- ylb:多表的连接与练习(第三方关联表的应用)
ylbtech-SQL Server:SQL Server-多表的连接与练习(第三方关联表的应用) SQL Server 多表的连接与练习(第三方关联表的应用). 1,多表的连接与练习(第三方关联表的 ...
- hadoop大数据处理之表与表的连接
hadoop大数据处理之表与表的连接 前言: hadoop中表连接其实类似于我们用sqlserver对数据进行跨表查询时运用的inner join一样,两个连接的数据要有关系连接起来,中间必须有一个 ...
- 小谈SQL表的连接
简述SQL连接 SQL连接呢,主要分为以下几种内连接,左连接,右连接,全连接(当然还有很多官方的说法,这里就讲讲最常用的). 既然都叫连接了,那至少要有两个对象,也就是说,至少要有两个表,要怎么样的表 ...
随机推荐
- Kafka核心组件详解
1.概述 对于Kafka的学习,在研究其系统模块时,有些核心组件是指的我们去了解.今天给大家来剖析一下Kafka的一些核心组件,让大家能够更好的理解Kafka的运作流程. 2.内容 Kafka系统设计 ...
- 如何在Linux上恢复误删除的文件或目录
Linux不像windows有那么显眼的回收站,不是简单的还原就可以了.linux删除文件还原可以分为两种情况,一种是删除以后在进程存在删除信息,一种是删除以后进程都找不到,只有借助于工具还原,这里分 ...
- 流量录制回放工具jvm-sandbox-repeater入门篇——服务部署
趋于当前技术不断更新.产品功能多元化之下,流量回放的热度也是越来越高. 在前一段时间,测试团队也提到阿里开源的流量回放工具 jvm-sandbox-repeater 我个人就先尝试一下,期间还是遇到一 ...
- 解析Java-throw抛出异常详细过程
摘要:Java有3种抛出异常的形式:throw.throws.系统自动抛异常. 本文分享自华为云社区<Java-throw异常详解以及过程>,作者: gentle_zhou . 首先,我们 ...
- 有关 ThreadLocal 的一切
早上好,各位新老读者们,我是七淅(xī). 今天和大家分享的是面试常驻嘉宾:ThreadLocal 当初鹅厂一面就有问到它,问题的答案在下面正文的第 2 点. 1. 底层结构 ThreadLocal ...
- hashlib加密模块和logging模块,购物车项目
hashlib加密模块 简介 hashlib模块是一个提供了字符串加密功能的模块,包含MD5和SHA的加密算法.具体的加密支持有: MD5,sha1,sha224,sha256, sha384, sh ...
- MySQL存储过程入门了解
0.环境说明: mysql版本:5.7 1.使用说明 存储过程是数据库的一个重要的对象,可以封装SQL语句集,可以用来完成一些较复杂的业务逻辑,并且可以入参出参(类似于java中的方法的书写). ...
- 关于linux多线程fork的理解和学习
fork在英文中是"分叉"的意思.为什么取这个名字呢?因为一个进程在运行中,如果使用了fork函数,就产生了另一个进程,于是进程就"分叉"了,所以这个名字取得很 ...
- 使用python获取交换机syslog日志并使用jQuery在html上展示
需求 现网有部分pop点独立于海外,无法发送日志给内网日志服务器,同时最近网内有比较重要割接,所以临时写一个脚本来展示网内日志 思路 使用socket接收syslog数据,udp 514,数据部分格式 ...
- 一些GIT操作的技巧
一.git stash 我们有时会遇到这样的情况,正在分支a上开发一半,然后分支b上发现Bug,需要马上处理.这时候分支a上的修改怎么办呢,git add 是不行的,有的git客户端版本会提示还有ad ...