查询日期顺序中的连续结果

时间:2017-03-15 23:05:59

标签: php mysql

版本:Mysql 5.5,PHP 5.4

我有一个竞赛结果数据库。

表结构如下

id(INT),date(日期),pointsfor(TINYINT),pointsagainst(TINYINT)

总结一下,if pointsfor > pointsagainst,该日期的结果被视为胜利。

我希望展示的结果是' winningest'跑,即连续记录的最多胜利的开始和结束日期,最好的连胜"。

我不知道从哪里开始,因为我还没有处理日期。

2 个答案:

答案 0 :(得分:0)

好的,我已经编写了一些我认为可以解决问题的基本代码,但是我还没有执行它,因为我没有你的数据集。

$query = "select * from competition_results order by date;"

$result = mysql_query($query);

// First we'll iterate over all the games and build up an array of streaks

$streak_num = 0;      // an ID we will increment for each streak
$winning = false;     // flag indicating whether we're in a streak or not
$streaks = array();   // game data for all winning streaks

while($row = mysql_fetch_assoc($result)) {
  if(!$winning) {
    // we have been losing up until now

    if($row['pointsfor'] - $row['pointsagainst'] > 0) {
      // hey we just won, we need to start a new streak and add this to it
      $streak_num++;
      $winning = true;
      $streaks[$streak_num] = array();
      $streaks[$streak_num][] = $row;
    }
  } else {
    // we're in the midst of a winning streak
    if($row['pointsfor'] - $row['pointsagainst'] < 0) {
      // we just lost a game, breaking this streak
      $winning = false;
    } else if ($row['pointsfor'] - $row['pointsagainst'] > 0) {
      // awesome we won another one in this streak
      $streaks[$streak_num][] = $row;
    }
  }
}

// Then we'll iterate over the win streaks to find the longest one

$winningest_streak = 0;
$most_wins = 0;
foreach($streaks as $streak_num => $streak_data) {
  $wins = count($streak_data);
  if($wins > $most_wins) {
    $most_wins = $wins;
    $winningest_streak = $streak_num;
  }
}

// print the game data for the best streak
print_r($streaks[$winningest_streak]);

我相信在这一点上,$winningest_streak包含$streak_num最长连胜。 $streaks[$winningest_streak]应该包含该连胜中涉及的所有游戏。

答案 1 :(得分:0)

我想尝试仅使用MySQL找到解决方案,但它有点过于复杂。所以这是PHP中的一些东西。

使用PDO

<?php

// Connect to database
$dsn = 'mysql:dbname=stackoverflow;host=127.0.0.1';
$user = 'root';
$password = 'password';

try {
    $database = new PDO($dsn, $user, $password);
} 
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

// Prepare and execute query
$statement = $database->prepare('SELECT * FROM `results` ORDER BY `date` ASC');
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);

// Prepare streaks
$streaks = $streak = [];

// Iterate through results and create winning streaks
foreach ($results as $result) {
    // Check if result was a win
    if ($result['for'] > $result['against']) {
        $streak['from'] = isset($streak['from']) ? $streak['from'] : $result['date'];
        $streak['to'] = $result['date'];
        $streak['wins'] = isset($streak['wins']) ? $streak['wins'] + 1 : 1;
    }
    // Append current streak to streaks
    else if ($streak) {
        array_push($streaks, $streak);
        $streak = [];
    }
}

// Find longest running streak
$longest = array_reduce($streaks, function ($longest, $streak) {
    return ($streak['wins'] >= $longest['wins']) ? $streak : $longest;
}, reset($streaks));

echo '<pre>'; print_r($longest); echo '</pre>';

使用MySQLi

<?php

// Connect to database
$mysqli = new mysqli('localhost', 'root', 'password', 'stackoverflow');

// Prepare and execute query
$results = $mysqli->query('SELECT * FROM `results` ORDER BY `date` ASC');

// Prepare streaks
$streaks = $streak = [];

// Iterate through results and create winning streaks
while (($result = $results->fetch_assoc())) {
    // Check if result was a win
    if ($result['for'] > $result['against']) {
        $streak['from'] = isset($streak['from']) ? $streak['from'] : $result['date'];
        $streak['to'] = $result['date'];
        $streak['wins'] = isset($streak['wins']) ? $streak['wins'] + 1 : 1;
    }
    // Append current streak to streaks
    else if ($streak) {
        array_push($streaks, $streak);
        $streak = [];
    }
}

// Find longest running streak
$longest = array_reduce($streaks, function ($longest, $streak) {
    return ($streak['wins'] >= $longest['wins']) ? $streak : $longest;
}, reset($streaks));

echo '<pre>'; print_r($longest); echo '</pre>';