通过查询获得最长连胜纪录

时间:2013-11-17 08:42:26

标签: mysql sql gaps-and-islands

我有一张如下表:

id     homeTeam    awayTeam   homeScore    awayScore
1         t1          t2          3            2
2         t3          t7          0            1
.          .           .          .            .
.          .           .          .            .
.          .           .          .            .

这是一项来自loocal联赛的足球比赛结果。 我想获得“最长连胜”,“最长连败”和........只需一个查询。我环顾四周找到了oracle版本,但我找不到怎么做? PS:我有mysql数据库。 提前谢谢

1 个答案:

答案 0 :(得分:5)

这是一种方式,但我有一种感觉,你不会喜欢它......

考虑以下数据DDL ......

CREATE TABLE results
(id     INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,homeTeam    INT NOT NULL
,awayTeam    INT NOT NULL
,homeScore    INT NOT NULL
,awayScore INT NOT NULL
);

INSERT INTO results VALUES
(1,1,2,3,2),
(2,3,4,0,1),
(3,2,1,2,0),
(4,4,3,1,0),
(5,3,2,1,2),
(6,2,3,0,2),
(7,1,4,4,1),
(8,4,1,1,2),
(9,1,3,3,0),
(10,3,1,1,0),
(11,4,2,1,0),
(12,2,4,1,2);

从这里,我们可以获得如下的中间结果......

SELECT x.*, COUNT(*) rank
  FROM
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) x
  JOIN 
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) y
    ON y.team = x.team
   AND y.id <= x.id
 GROUP
    BY x.id
     , x.team
 ORDER
    BY team, rank;

+----+------+--------+------+
| id | team | result | rank |
+----+------+--------+------+
|  1 |    1 | w      |    1 |
|  3 |    1 | l      |    2 |
|  7 |    1 | w      |    3 |
|  8 |    1 | w      |    4 |
|  9 |    1 | w      |    5 |
| 10 |    1 | l      |    6 |
|  1 |    2 | l      |    1 |
|  3 |    2 | w      |    2 |
|  5 |    2 | w      |    3 |
|  6 |    2 | l      |    4 |
| 11 |    2 | l      |    5 |
| 12 |    2 | l      |    6 |
|  2 |    3 | l      |    1 |
|  4 |    3 | l      |    2 |
|  5 |    3 | l      |    3 |
|  6 |    3 | w      |    4 |
|  9 |    3 | l      |    5 |
| 10 |    3 | w      |    6 |
|  2 |    4 | w      |    1 |
|  4 |    4 | w      |    2 |
|  7 |    4 | l      |    3 |
|  8 |    4 | l      |    4 |
| 11 |    4 | w      |    5 |
| 12 |    4 | w      |    6 |
+----+------+--------+------+

通过检查,我们可以看到球队1的连胜纪录最长(连续3次'w')。你可以设置几个@vars来追踪这个,或者如果你有点自虐(比如我),你可以做一些更慢,更长,更复杂的事情......

SELECT a.team
     , MIN(c.rank) - a.rank + 1 streak
  FROM (SELECT x.*, COUNT(*) rank
  FROM
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) x
  JOIN 
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) y
    ON y.team = x.team
   AND y.id <= x.id
 GROUP
    BY x.id
     , x.team
     ) a
  LEFT 
  JOIN (SELECT x.*, COUNT(*) rank
  FROM
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) x
  JOIN 
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) y
    ON y.team = x.team
   AND y.id <= x.id
 GROUP
    BY x.id
     , x.team
     ) b 
    ON b.team = a.team
   AND b.rank = a.rank - 1 
   AND b.result = a.result
  LEFT 
  JOIN (SELECT x.*, COUNT(*) rank
  FROM
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) x
  JOIN 
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) y
    ON y.team = x.team
   AND y.id <= x.id
 GROUP
    BY x.id
     , x.team
     ) c 
    ON c.team = a.team
   AND c.rank >= a.rank 
   AND c.result = a.result
  LEFT 
  JOIN (SELECT x.*, COUNT(*) rank
  FROM
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) x
  JOIN 
     ( SELECT id,hometeam team, CASE WHEN homescore > awayscore THEN 'w' ELSE 'l' END result FROM results 
       UNION
       SELECT id,awayteam, CASE WHEN awayscore > homescore THEN 'w' ELSE 'l' END result FROM results
     ) y
    ON y.team = x.team
   AND y.id <= x.id
 GROUP
    BY x.id
     , x.team
     ) d 
    ON d.team = a.team
   AND d.rank = c.rank + 1 
   AND d.result = a.result
 WHERE a.result = 'w'
   AND b.id IS NULL
   AND c.id IS NOT NULL
   AND d.id IS NULL
 GROUP 
    BY a.team
     , a.rank
 ORDER 
    BY streak DESC 
 LIMIT 1; 

 +------+--------+
 | team | streak |
 +------+--------+
 |    1 |      3 |
 +------+--------+

请注意,这并不考虑个别匹配关系(对重复子查询的适度更改),也不考虑两个团队是否具有相同长度的最长连胜条件(需要在此处重新连接所有内容!)。