SQL:case when用法简介、相关案例
目录
参考文章:如何学习 SQL 语言? - 知乎
case when用法简介
相关案例
1、列出每场比赛,每个队的进球数
select
b.mdate,
b.team1,
sum(case when teamid=team1 then 1 else 0 end) as score1,
b.team2,
sum(case when teamid=team2 then 1 else 0 end) as score2
from
(select matchid,teamid from goal) a
right join
(select id ,mdate,team1, team2 from game) b
on a.matchid = b.id
group by b.mdate, b.team1,b.team2
order by b.mdate,a.matchid, b.team1,b.team2 ;
--case when then else用法,多条件判段
--group by 分组