我有三张桌子Match_Event
,Team_Detail
和Match_Schedule
。
有Match_Schedule
列MatchId
, Hometeam_Id
, Away TeamId
:
matchId HTeam Ateam
----------------------
123 -1 -7
231 -3 -5
有与那场比赛有关的Match_event
事情。(监狱,犯规等)
matchId TeamId Score
--------------------------------
123 1 1
123 1 1
123 7 1
231 5 1
最后一张表 ( TeamDetail
)
TeamId TeamName
123 USA
231 Uk
我试过这个
SELECT
Team_Detail.Team_Name,
SUM(CASE WHEN Team_Detail.Serno = Match_Events.Team_Id THEN 1 ELSE 0 END) AS HomeTeamScore,
Team_Detail.Team_Name,
SUM(CASE WHEN Team_Detail.Serno = Match_Events.Team_Id THEN 1 ELSE 0 END) AS AwayTeamScore
FROM
Match_Schedule
INNER JOIN
Match_Events ON Match_Schedule.Match_Serno = Match_Events.Match_ID
INNER JOIN
Team_Detail ON Match_Events.Team_Id = Team_Detail.Serno
GROUP BY
Team_Name, Team_Name
我得到了正确的结果,但一个高于另一个而不是在同一行。
有人可以帮帮我吗。
谢谢