sqlzoo:6
第一個例子列出球員姓氏為'Bender'的入球數據。 * 表示列出表格的全部欄位,簡化了寫matchid, teamid, player, gtime語句。
修改此SQL以列出 賽事編號matchid 和球員名 player ,該球員代表德國隊Germany入球的。要找出德國隊球員,要檢查: teamid = 'GER'
SELECT matchid,player FROM goal
WHERE teamid='GER'
由以上查詢,你可見Lars Bender's 於賽事 1012入球。.現在我們想知道此賽事的對賽隊伍是哪一隊。
留意在 goal 表格中的欄位 matchid ,是對應表格game的欄位id。我們可以在表格 game中找出賽事1012的資料。
只顯示賽事1012的 id, stadium, team1, team2
SELECT id,stadium,team1,team2
FROM game
where id=1012
我們可以利用JOIN來同時進行以上兩個步驟。
以下SQL列出每個入球的球員(來自goal表格)和場館名(來自game表格)
修改它來顯示每一個德國入球的球員名,隊伍名,場館和日期。
SELECT player,teamid,stadium,mdate
FROM game JOIN goal ON (id=matchid)
where teamid='GER'
列出球員名字叫Mario (player LIKE 'Mario%')有入球的 隊伍1 team1, 隊伍2 team2 和 球員名 player
select team1,team2,player
from game join goal on(id=matchid)
where player like 'Mario%'
注意欄位id同時是表格game 和表格 eteam的欄位,你要清楚指出eteam.id而不是只用id
列出'Fernando Santos'作為隊伍1 team1 的教練的賽事日期,和隊伍名。
select mdate,teamname
from game join eteam on team1=eteam.id
where coach='Fernando Santos'
列出場館 'National Stadium, Warsaw'的入球球員。
select player
from game join goal on id=matchid
where stadium='National Stadium, Warsaw'
修改它,只列出全部賽事,射入德國龍門的球員名字。
SELECT DISTINCT player
FROM game JOIN goal ON matchid = id
WHERE (team1='GER' OR team2='GER') AND (teamid != 'GER')
列出隊伍名稱 teamname 和該隊入球總數
SELECT teamname, count(gtime)
FROM eteam JOIN goal ON id=teamid
group by teamname
列出場館名和在該場館的入球數字。
select stadium,count(gtime)
from game join goal on id=matchid
group by stadium
每一場波蘭'POL'有參與的賽事中,列出賽事編號 matchid, 日期date 和入球數字。
SELECT matchid,mdate,count(gtime)
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL')
group by matchid,mdate
每一場德國'GER'有參與的賽事中,列出賽事編號 matchid, 日期date 和德國的入球數字。
select matchid,mdate,count(teamid)
from game join goal on matchid=id
where teamid='GER'
group by matchid,mdate
| mdate | team1 | score1 | team2 | score2 |
|---|---|---|---|---|
| 1 July 2012 | ESP | 4 | ITA | 0 |
| 10 June 2012 | ESP | 1 | ITA | 1 |
| 10 June 2012 | IRL | 1 | CRO | 3 |
| ... | ||||
Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.
select mdate,
team1,
sum(case when teamid=team1 then 1 else 0 end)score1,
team2,
sum(case when teamid=team2 then 1 else 0 end)score2
from game left JOIN goal ON matchid = id
group by mdate,matchid,team1,team2
sqlzoo:6的更多相关文章
- 练习sql语句的好去处——http://www.sqlzoo.cn/
sql语句的编写需要按照实际的例子来练习. 如果自己来做准备,需要你自己搭好数据库,建好库和表,还要填入数据,最后自己想出题目和正确答案. 不过,现在我发现了一个好去处,http://www.sqlz ...
- sqlzoo练习题答案
title: SQL-Learning date: 2019-03-12 20:37:21 tags: SQL --- 这是关于在一个SQL学习网站的练习题答案记录:SQL教程 SQL基础 由一些简单 ...
- sqlzoo易错题
https://sqlzoo.net/wiki/SELECT_names 答案在:https://github.com/codyloyd/sqlzoo-solutions/blob/master/SQ ...
- SQLZOO 习题
https://sqlzoo.net 8. 美國.印度和中國(USA, India, China)是人口又大,同時面積又大的國家.排除這些國家. 顯示以人口或面積為大國的國家,但不能同時兩者.顯示國家 ...
- More JOIN operations -- SQLZOO
The JOIN operation 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List the films where the y ...
- The JOIN operation -- SQLZOO
The JOIN operation 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Modify it to show the matc ...
- SUM and COUNT -- SQLZOO
SUM and COUNT 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Show the total population of th ...
- SELECT within SELECT Tutorial -- SQLZOO
SELECT within SELECT Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List each count ...
- sqlzoo - SELECT from WORLD Tutorial 答案
01.SELECT from WORLD Tutorial 01.显示所有国家的名称,大洲和人口. SELECT name, continent, population FROM world; 02. ...
- sqlzoo.net刷题5
List the continents that have a total population of at least 100 million. 这题考察的是使用集聚函数生成表之后,如何过滤 一般我 ...
随机推荐
- python常用的内置函数
python常用的内置函数集合做一个归类用的时候可以查找- abs 返回数字x的绝对值或者x的摸 - all (iterable)对于可迭代的对象iterable中所有元素x都有bool(x)为tru ...
- sorted
排序是编程中经常使用到的算法,无论哪种排序算法, 本质上都是比较两个元素的大小.如果是数字,可以直接比较,但是如果是字符串或者是dict呢?直接比较数学上的大小是没有意义的,因此,比较的过程必须通过函 ...
- 【3】学习C++之const关键字的使用
在C++中,const关键字是控制变量是否可以变化的,是否能够用好const关键字是区别小白和大佬的重要指标(大雾). 1.const与基本数据类型 ; //a是变量,a的值可以在后续操作中进行更改. ...
- Arduino语言简介
参考链接:https://www.cnblogs.com/xczr/p/7831343.html
- recurrent model for visual attention
paper url: https://papers.nips.cc/paper/5542-recurrent-models-of-visual-attention.pdf year: 2014 abs ...
- MySQL5.7开启独立表空间参数innodb_file_per_table【原创】
今天在线上某个系统发现MySQL数据库使用的是共享表空间,想修改为独立表空间,操作如下: #因为是主从结构,在从库修改测试,先关闭binlog SET SQL_LOG_BIN=; show varia ...
- @PostConstruct和@PreDestroy注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- 变分自编码器(Variational Autoencoder, VAE)通俗教程
原文地址:http://www.dengfanxin.cn/?p=334 1. 神秘变量与数据集 现在有一个数据集DX(dataset, 也可以叫datapoints),每个数据也称为数据点.我们假定 ...
- 虎牙直播弹幕转换字幕格式 基于Node.js 的 huya-danmu
1 首先安装nodejs运行环境, 从 http://nodejs.cn/download/ 下载对应的版本 2 安装 huya-danmu 模块, https://github.com/BacooT ...
- Python爬虫基础之UrlError
一.urllib.error python的urllib.error模块主要是应对urllib.request在网络请求过程中出现的异常而定义的异常处理类.主要有URLError和HTTPError两 ...