版本:MySql 5.5,PHP 5.4
以下查询返回足球队的5个最近比赛日期,以及他们的净胜球和比赛ID。
$get_form = $conn->query("
SELECT (GoalsFor - GoalsAgainst) AS deficit, MatchID AS matchid
FROM matches
WHERE HomeTeamID = $homeid AND MatchDate < '$today'
ORDER BY MatchDate DESC
LIMIT 5
");
我正在使用下面显示的fetch来使用PHP检索5个结果。
while($data = $get_form->fetch_assoc())
{
if($data['deficit'] < 0)
echo"Win ";
else
echo"Loss ";
}
但结果目前按照从左到右的降序排列。我宁愿让它们按照从右到左的升序顺序出现。
我建议将ORDER BY更改为ASC,但这只会返回五个最老的匹配。我只希望在结果集中返回最近的五个(在今天的日期之前)。
答案 0 :(得分:2)
SELECT *
FROM (
SELECT (GoalsFor - GoalsAgainst) AS deficit, MatchID AS matchid
FROM matches
WHERE HomeTeamID = $homeid AND MatchDate < ?
ORDER BY MatchDate DESC
LIMIT 5
) X
ORDER BY MatchDate;