上一个和下一个匹配MySQL

cyej8jka  于 12个月前  发布在  Mysql
关注(0)|答案(1)|浏览(140)

我有一个足球赛程表,我需要根据日期返回上一场和下一场比赛。有没有一个简单的方法来做到这一点?该表有日期,时间,团队,所以需要按团队分组。这就是我目前所做的,以获得下一个夹具。
任何帮助将不胜感激:)

3pmvbmvn

3pmvbmvn1#

你必须用像php这样的脚本语言来做这件事,至少我会这样做。我不是一个专业的开发人员,但我写了快速以下php脚本,得到以前的匹配出mysql数据库。你可以为下一场比赛做类似的事情。也许有一个更容易的方法来做到这一点,但这个脚本的工作...

<?php

/*get the current day*/
$day = date("d");

$previous = array();
$j = 0; /*this variable will say on which index we have to store the team name in the array previous*/
$k = 1; /*this variable will say how much days we have to count back*/

/*now we get the previous math out of the database be counting down the days and looking if there is a record*/
for($i=$day; $i>0; $i--) {
    $date = date("dmY", strtotime("-$k days"));

    $result = mysql_query("SELECT * FROM `football` WHERE `date`='$date'") or die (mysql_error());
    $num = mysql_num_rows($result);
    $fetch = mysql_fetch_array($result);

    if($num == 1) {
        $previous[$j] = $fetch["team"];
        $j++;
    }
    $k++;
}

echo $previous[0];

?>

相关问题