Mysql - 连续计算日期与条件和获取范围列表

时间:2015-08-15 02:42:47

标签: mysql date count rows

我需要知道球队连续多少次赢得比赛(value1> value2),并知道第一场比赛和最后一场比赛的胜利日期。并显示按赢额和日期排列的10个范围的列表。 我知道这并不容易。这对我来说并不容易..我尝试做一些查询和子查询,但没有很好的结果。谢谢 我有一张桌子

            CREATE TABLE `games` (
              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `value1` int(11) NOT NULL,
              `value2` int(11) NOT NULL,
              `played` date NOT NULL,
              PRIMARY KEY (`id`),
              KEY `played` (`played`)
            ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

            insert into games (value1, value2, played)
            values
            ("2", "3", "1943-05-09"),
            ("4", "3", "1943-08-15"),
            ("1", "8", "1943-08-22"),
            ("0", "4", "1943-08-29"),
            ("1", "0", "1943-09-12"),
            ("1", "3", "1943-09-26"),
            ("6", "1", "1943-10-03"),
            ("3", "2", "1943-10-10"),
            ("3", "3", "1944-07-16"),
            ("1", "1", "1944-08-06"),
            ("4", "1", "1944-09-24"),
            ("0", "7", "1944-10-08"),
            ("0", "1", "1945-05-13"), // 1
            ("4", "2", "1945-11-04"), // 2
            ("3", "2", "1946-05-12"), // 3 second 3 consecutives win
            ("4", "2", "1946-11-17"),
            ("2", "2", "1946-11-24"),
            ("1", "5", "1946-12-01"),
            ("1", "0", "1947-05-18"),
            ("3", "0", "1947-10-05"),
            ("2", "3", "1948-11-07"),
            ("0", "1", "1948-11-14"),
            ("1", "4", "1948-11-21"),
            ("3", "1", "1949-06-12"), // 1
            ("4", "0", "1949-06-19"), // 2
            ("5", "1", "1949-07-24"), // 3
            ("3", "1", "1949-08-06")  // 4 first consecutives win

我需要这样的结果

            From            To          games_won

            1949-06-12  1949-08-06          4
            1945-11-04  1946-11-17          3
            1943-10-03  1943-10-10          2
            ...

...

0 个答案:

没有答案