第一個例子列出球員姓氏為'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. SAVEPOINT 标记

    create table duo(               --创建表格                v_xuhao number(3),                v_name varch ...

  2. Spring.Net 入门学习笔记-----one

    一. 基本概念    Spring.Net是一个轻量级的控制反转(Ioc)和面向切面的(Aop)的容器框架: Ioc:控制反转:简单的说就是将创建对象的控制权转交给外部容器(IApplicationC ...

  3. 初始化仓库(git init)

    创建新的仓库 首先进入需要初始化的目录,然后输入git init D:\Git\test λ git init Initialized empty Git repository in D:/Git/t ...

  4. 常用软件记录 ( Windows )

    带括号注明的为主力软件 现安装的: 360驱动大师 7-Zip Acrobat Reader DC Adobe Photoshop 7.0 Anaconda babun Bandizip (解压) b ...

  5. idea如何快速查看接口的实现类

    查找接口的实现类: IDEA 风格 ctrl + alt +B 在按F2查看详细文档注解 查看类或接口的继承关系: ctrl + h

  6. net core appsetting配置

    public class BaseController : Controller { protected WLEntity _db; protected ILogger _log; protected ...

  7. java连接3种数据库 JdbcLinkDB --201801

    先看这篇记录 java连接3种数据库 JdbcLinkDB 测试 --201801 配置文件放在jar外面 读取,遇到的问题 - 海蓝steven - 博客园https://www.cnblogs.c ...

  8. vue 前端框架 (三)

    VUE 生命周期 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  9. JAVA实例

     JAVA实例1  1 package Demo3; import java.io.File; import java.io.FileReader; import java.io.IOExceptio ...

  10. 深度学习之自编码器AutoEncoder

    原文地址:https://blog.csdn.net/marsjhao/article/details/73480859 一.什么是自编码器(Autoencoder) 自动编码器是一种数据的压缩算法, ...