sqlzoo-练习6——The JOIN operation

sqlzoo-练习6——The JOIN operation

sqlzoo-The JOIN operation: https://sqlzoo.net/wiki/The_JOIN_operation

这期主要是使用 JOIN 进行 多表查询


JOIN and UEFA EURO 2012
This tutorial introduces JOIN which allows you to use data from two or more tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.
The data is available (mysql format) at http://sqlzoo.net/euro2012.sql


1、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、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 that the matchid column 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 = 
	(SELECT matchid FROM goal
	WHERE player = 'lars bender')
  • SELECT 嵌套, 可以查询不同表格。
  • 嵌套SELECT可能速度更慢一些。这期使用JOIN,将两个步骤合为一个,效率更高。

### 重点 :You can combine the two steps into a single query with a JOIN…ON….

 SELECT *
 FROM game JOIN goal ON (id=matchid)

The FROM clause says to merge data from the goal t

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值