2017-06-29 94 views
0

版本:5.5的MySQL,PHP 5.4反向從MySQL陣列的結果取

下面的查詢返回的橄欖球隊,以及他們的目標赤字,並匹配ID 5個最近的比賽日期。

$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 
"); 

我使用下面顯示的獲取使用PHP檢索5個結果。

while($data = $get_form->fetch_assoc()) 
    { 
    if($data['deficit'] < 0) 
     echo"Win "; 
    else 
     echo"Loss "; 
    } 

但結果目前按降序排列,從左到右依次出現。我寧願讓他們按照時間順序從右到左出現。

已經向我推薦我將ORDER BY更改爲ASC,但這隻會返回五個最早的匹配項。我只想在結果集中返回最近的五個(今天的日期之前)。

+1

'select(by select)by' –

+1

這對此有幫助嗎? https://stackoverflow.com/questions/23456172/echo-in-reverse-order-from-mysql-fetch-assoc – rbaskam

回答

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;