SQL学习四(SQL ZOO - JOIN)

本文介绍两个实用的SQL查询案例:一是列出所有非德国队球员在对阵德国队比赛中进球的名单;二是展示每场比赛中两支队伍的得分情况,通过使用CASE WHEN语句实现对比赛结果的详细统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、THE JOIN OPERATION

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.

HINT

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(player) from goal join game on matchid = id where teamid != 'GER' and(team1 = 'GER' or team2 = 'GER')

 

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.

mdateteam1score1team2score2
1 July 2012ESP4ITA0
10 June 2012ESP1ITA1
10 June 2012IRL1CRO3
...

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 id = matchid group by mdate, matchid, team1, team2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值