在mySQL中使用COUNT很困难

时间:2017-03-08 17:29:02

标签: mysql count

我有一个壁球锦标赛比赛分数的数据库,如下所示:

MatchResult的

enter image description here

GameResult

enter image description here

通过以下结果,您可以收集到A队击败B队:

蝙蝠侠3-0:11-1,11-2,11-3击败超人。

穆法萨3-0:11-1,11-1,11-1击败了伤病

我可以通过选择这些

来获得每支球队赢得/输掉的积分和比赛

积分

(Select((SELECT IFNULL((SELECT SUM(GRA.PlayerAPoints)
    FROM GameResult GRA
    JOIN MatchResult mA ON GRA.MatchResultId = mA.Id
    WHERE mA.PlayerATeamId = team.Id),0))
+    
(SELECT IFNULL((SELECT SUM(GRB.PlayerBPoints)
    FROM GameResult GRB
    JOIN MatchResult mB ON GRB.MatchResultId = mB.Id
    WHERE mB.PlayerBTeamId = team.Id),0)))) AS 'PointsWonA'

GamesWon

(Select(SELECT IFNULL((SELECT COUNT(*)
                        FROM GameResult GRA
                        JOIN MatchResult mA ON GRA.MatchResultId = mA.Id
                        Where GRA.PlayerAPoints > GRA.PlayerBPoints
                        AND mA.PlayerATeamId = team.Id)
                    ,0)
        )
        +
        (SELECT IFNULL((SELECT COUNT(*)
                                FROM GameResult GRB
                                JOIN MatchResult mB ON GRB.MatchResultId = mB.Id
                                Where GRB.PlayerBPoints > GRB.PlayerAPoints
                                AND mB.PlayerBTeamId = team.Id)
                            ,0)
                )       
) AS 'GamesWonA'

但是,我正在努力解决如何获得每个球队赢得的比赛数。有什么建议吗?

0 个答案:

没有答案