首先题目是这样的:

球队以及得分计算的SQL语句-LMLPHP

球队表teams

比赛表matches

赢了得3分,平局的得1分,输了得0分。

思路:

一个球队的成绩分为两部分,作为主队的得分和作为客队的得分;

计算出一次比赛中具体得了多少分,具体的比较我也不知道;

得到这个零时表然后根据球队group求和;

最后根据球队表关联得出球队的名字;

然后在排个序。

select teams.*,
ISNULL(points.num_points,0) as num_points
from teams left join
(select host_team,
SUM(hostpoint) as num_points
from (select host_team,
(case when host_goals>guest_goals then 3 when host_goals=guest_goals then 1 else 0 end) as hostpoint
from matches
union
select guest_team,
(case when host_goals>guest_goals then 0 when host_goals=guest_goals then 1 else 3 end) as guestpoint
from matches) point
group by host_team) points
on teams.team_id=points.host_team
order by points.num_points desc,teams.team_id asc

1. 我不知道怎么把比赛结果转化为得分,case when还不会用,不知道还有这个

2. ISNULL也没有判断

05-11 17:21