第一個例子列出球員姓氏為'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'
以下例子找出德國-希臘Germany-Greece 的八強賽事的入球

修改它,只列出全部賽事,射入德國龍門的球員名字。

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
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.
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的更多相关文章

  1. 练习sql语句的好去处——http://www.sqlzoo.cn/

    sql语句的编写需要按照实际的例子来练习. 如果自己来做准备,需要你自己搭好数据库,建好库和表,还要填入数据,最后自己想出题目和正确答案. 不过,现在我发现了一个好去处,http://www.sqlz ...

  2. sqlzoo练习题答案

    title: SQL-Learning date: 2019-03-12 20:37:21 tags: SQL --- 这是关于在一个SQL学习网站的练习题答案记录:SQL教程 SQL基础 由一些简单 ...

  3. sqlzoo易错题

    https://sqlzoo.net/wiki/SELECT_names 答案在:https://github.com/codyloyd/sqlzoo-solutions/blob/master/SQ ...

  4. SQLZOO 习题

    https://sqlzoo.net 8. 美國.印度和中國(USA, India, China)是人口又大,同時面積又大的國家.排除這些國家. 顯示以人口或面積為大國的國家,但不能同時兩者.顯示國家 ...

  5. More JOIN operations -- SQLZOO

    The JOIN operation 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List the films where the y ...

  6. The JOIN operation -- SQLZOO

    The JOIN operation 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Modify it to show the matc ...

  7. SUM and COUNT -- SQLZOO

    SUM and COUNT 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Show the total population of th ...

  8. SELECT within SELECT Tutorial -- SQLZOO

    SELECT within SELECT Tutorial 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.List each count ...

  9. sqlzoo - SELECT from WORLD Tutorial 答案

    01.SELECT from WORLD Tutorial 01.显示所有国家的名称,大洲和人口. SELECT name, continent, population FROM world; 02. ...

  10. sqlzoo.net刷题5

    List the continents that have a total population of at least 100 million. 这题考察的是使用集聚函数生成表之后,如何过滤 一般我 ...

随机推荐

  1. Hyper-V安装虚拟机

    1.进入控制面板-程序,选择“启用或关闭Windows功能” 2.找到Hyper-V,勾选,重启电脑,即可 3.若是没有找到Hyper-V,如此操作 1)新建txt文件,输入以下内容: pushd & ...

  2. 【转载】大白话Docker入门(一)

    原文:https://yq.aliyun.com/articles/63035 随着docker现在越来越热门,自己也对docker的好奇心也越来越重,终于忍不住利用了一些时间把docker学习一遍. ...

  3. git本机服务器配置(一):git的安装

    1. 下载git的Windows版本 https://gitforwindows.org/ 2. 安装 点击next 不需要更改,直接点击next 配置git编辑器,我安装了notepad++所以就选 ...

  4. A previous installation of Qt5 Visual Studio Add-in was detected. Please uninstall it before running this installer解决办法

    前段时间在安装Qt Visual Studio插件的时候,安装到一半不小心中止了,结果后来怎么安装都不行,提示已经安装了,要先卸载, 可是到哪里都找不到有卸载的,因为压根就没有安装完成.这可害苦我了. ...

  5. 【转】Oracle之索引

    简介 1.说明 1)索引是数据库对象之一,用于加快数据的检索,类似于书籍的目录.在数据库中索引可以减少数据库程序查询结果时需要读取的数据量,类似于在书籍中我们利用目录可以不用翻阅整本书即可找到想要的信 ...

  6. 逆元知识普及(扫盲篇) —— from Judge

    watch out 本文是博主的 csdn 上搬过来的,格式有点崩,看不下去的可以去 博主的 csdn上看(上面 格式会好很多,并且有些公式也用 $\LaTeX$  update 上去了) 最近有点颓 ...

  7. Vue + Element UI项目初始化

    1.安装相关组件 1.1安装Node 检查本地是否安装node node -v 如果没有安装,从Node官网下载 1.2安装npm npm -v 如果没有安装:使用该指令安装: npm install ...

  8. ORA-01466: 无法读取数据 - 表定义已更改

    前几天同事同事误删除数据,经查询发现数据在7:13分时候还是全量 628W行: 于是他将现在的表复制了个备份,其中有数据200W: 于是为了省事,想要直接闪回全表,就把这个表truncate了.... ...

  9. python使用正则解析网络地址的各个部分

    参考文章: https://www.cnblogs.com/ingd/p/6182571.htmlimport re def resolveAddress(url): ""&quo ...

  10. cocos2dx-lua调用C++

    文参考:https://www.cnblogs.com/xiaonanxia/p/4987856.html 上面的文章是IOS版教程,用4部分说明原理,1部分说操作步骤. 这里用window VS20 ...