mysql统计胜负以获得连胜

watbbzwu  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(275)

我有一张包含日期、队伍和结果的体育比赛表。我想找出一支球队目前的胜负记录。
我的table是这样的:

+------------+----------+---------+---------------+------+------------+
| date_month | date_day | visitor | visitor_score | home | home_score |
+------------+----------+---------+---------------+------+------------+
| May        | 1        | MyTeam  | 90            | Z    | 100        |
+------------+----------+---------+---------------+------+------------+
| April      | 26       | X       | 100           |MyTeam| 90         |
+------------+----------+---------+---------------+------+------------+
| April      | 21       | Y       | 90            |MyTeam| 100        |
+------------+----------+---------+---------------+------+------------+
| March      | 25       | MyTeam  | 90            | W    | 100        |
+------------+----------+---------+---------------+------+------------+

我已经完成了按日期排序结果。

SELECT * FROM Schedule WHERE visitor_score>0
            ORDER BY CASE
            when `date_month` = 'May' then 1
            when `date_month` = 'April' then 2
            when `date_month` = 'March' then 3
            else 4
            end asc, date_day desc

问题是,如何找到一支球队的连胜? Myteam 上两场比赛输了,所以应该是这样 0 赢家和 2 损失。但如果它赢了下一场比赛,它显然应该赢 1 赢家和 0 损失。
我知道如何提取赢家和输家(如下),但这是我一直坚持的观点。

WHERE (visitor = 'MyTeam' && visitor_score>home_score) OR (home = 'MyTeam' && home_score>visitor_score)
qqrboqgw

qqrboqgw1#

我不会抱怨你的table结构。我假定您不能更改表结构。
我已经完成了按日期排序结果。
你的解决方案很难看。考虑使用以下方法:

SELECT STR_TO_DATE(CONCAT(date_month, date_day, YEAR(CURDATE())),"%M%d%Y") AS date

请注意,我必须使用year(curdate()),因为在str\u to\u date函数中,year是必需的,而且您似乎没有存储游戏的年份。现在您可以简单地: ORDER BY date DESC 现在我来解释一下我想到的关于w/l/d(赢、输、平)连胜的第一个解决方案。
声明3个函数:
retrievelastresult(返回w,l,d)
retrievelastgamedate(返回日期\u月,日期\u日)
retrievefirstresultdatedifferentfromlastone(返回结果与上一个不同的第一个游戏的日期)
下面是每个函数体的模拟。
一旦你有了这些功能,你就可以简单地做这样的事情:

SELECT COUNT(*), retrieveLastResult 
FROM Schedule
WHERE
STR_TO_DATE(CONCAT(date_month, date_day, YEAR(CURDATE())),"%M%d%Y") 
    BETWEEN DATE_SUB(retrieveFirstResultDateDifferentFromLastOne(retrieveLastResult,retrieveLastGameDate),INTERVAL 1 DAY) 
       AND retrieveLastGameDate()

retrievelastresult函数

SELECT 
        IF(`home` = "MyTeam",  
            CASE 
                WHEN  `home_score` > `visitor_score` THEN "W"
                WHEN  `home_score` < `visitor_score` THEN "L"
                WHEN  `home_score` = `visitor_score` THEN "D"
            END,
            CASE 
                WHEN  `visitor_score` > `home_score` THEN "W"
                WHEN  `visitor_score` < `home_score` THEN "L"
                WHEN  `visitor_score` = `home_score` THEN "D"
            END
         )
FROM schedule
ORDER BY STR_TO_DATE(CONCAT(date_month, date_day, YEAR(CURDATE())),"%M%d%Y") DESC
LIMIT 1

retrievelastgamedate函数

SELECT STR_TO_DATE(CONCAT(date_month, date_day, YEAR(CURDATE())),"%M%d%Y") as date
FROM schedule
ORDER BY date
LIMIT 1

retrievefirstresultdatedifferentfromlastone函数

SELECT date
FROM schedule
WHERE 
    IF(`home` = "MyTeam",  
            CASE 
                WHEN  `home_score` > `visitor_score` THEN "W"
                WHEN  `home_score` < `visitor_score` THEN "L"
                WHEN  `home_score` = `visitor_score` THEN "D"
            END,
            CASE 
                WHEN  `visitor_score` > `home_score` THEN "W"
                WHEN  `visitor_score` < `home_score` THEN "L"
                WHEN  `visitor_score` = `home_score` THEN "D"
            END
        ) != lastResult()
ORDER BY STR_TO_DATE(CONCAT(date_month, date_day, YEAR(CURDATE())),"%M%d%Y") DESC
LIMIT 1

相关问题