2013-05-01 84 views
-1

之前訂購我做了一個高爾夫記分卡網站。我正在處理的頁面是玩家個人資料。當您訪問球員配置文件時,它會按照上次播放的順序(DESC)顯示每個球場。除了上面播放的順序由於下面的ORDER BY命令而混亂。相反,當它是GROUP時,它會採用最早的日期,而不是最近的日期。我怎樣才能得到這個數據庫在GROUP BY

分組完成後,它按順序正確顯示它們(DESC)...由於按照date_of_game ASC而不是DESC分組的課程,所以順序錯誤。希望這不是太混亂..謝謝。

$query_patrol321 = "SELECT t1.*,t2.* FROM games t1 LEFT JOIN scorecards t2 ON t1.game_id=t2.game_id WHERE t2.player_id='$player_id' GROUP BY t1.course_id ORDER BY t1.date_of_game DESC"; 
$result_patrol321 = mysql_query($query_patrol321) or die ("<br /><br />There's an error in the MySQL-query: ".mysql_error()); 
while ($row_patrol321 = mysql_fetch_array($result_patrol321)) { 
    $player_id_rank = $row_patrol321["player_id"]; 
    $course_id = $row_patrol321["course_id"]; 
    $game_id = $row_patrol321["game_id"]; 
    $top_score = $row_patrol321["total_score"]; 
+1

我完全不明白這個問題,但你可以嘗試改變順序爲'ORDER BY MAX(t1.date_of_game)DESC'?如果您試圖顯示每門課程的最後一次播放日期,這將起作用。 – 2013-05-01 17:16:36

+0

完美,謝謝。很高興這是一個簡單的修復。我無法弄清楚。 – Rmurp006 2013-05-01 17:21:29

+1

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-05-01 18:07:22

回答

2

嘗試從查詢中刪除GROUP BY子句。只有當您在SELECT中同時具有普通列和聚合函數(min,max,sum,avg,count)時,才應該使用GROUP BY。你只有普通的列。

+0

+1,沒有聚合的「GROUP BY」是SQL的誤用。 – Adrian 2013-05-01 17:35:19

+0

MySQL允許這種行爲。我一直覺得它很奇怪和令人困惑。我相信,如果沒有聚合,它會在聚合中獲得第一個條目。 – Matthew 2013-05-01 17:39:04

+0

儘管MySQL不會在沒有聚合函數+普通列的情況下使用GROUP BY時發生錯誤,但您確實不應該這樣使用它。總是有正確的方式來執行查詢。 – slaakso 2013-05-01 20:33:33

0

它顯示ASC順序的分組結果的事實是一個巧合,因爲這是它們插入的順序。與其他RDBMS(如MS SQL Server)相比,MySQL允許您將未彙總的列添加到GROUP ed查詢中。這種非標準的行爲會造成你所看到的混亂。如果這不是MySQL,您需要爲給定分組的所有選定列定義聚合。

MySQL的行爲是(我相信)採取第一行匹配非聚合列GROUP。我會建議不要這樣做。

即使您正在進行彙總,您也不是ORDER中的聚合列。

所以,你想要做的是ORDER BYMAX日期DESC

這樣,你是每門課程(您的分組標準)的最新日期排序。

SELECT 
    t1.* -- It would be better if you actually listed the aggregations you wanted 
    ,t2.* -- Which columns do you really want? 
FROM 
    games t1 
LEFT JOIN 
    scorecards t2 
     ON t2.[game_id] =t1[.game_id] 
WHERE 
    t2.[player_id]='$player_id' 
GROUP BY 
    t1.[course_id] 
ORDER BY 
    MAX(t1.[date_of_game]) DESC 
0

如果你想要的最大日期,然後插入邏輯來得到它。不要依賴列的順序或者沒有記錄的MySQL特性。 MySQL的明確鼓勵使用在group by非聚合列當值不相同:

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. (see [here][1]) 

你如何你想做些什麼?以下查詢查找在每門課程的最近日期,只是使用了 - 沒有group by

SELECT t1.*, t2.* 
FROM games t1 LEFT JOIN 
    scorecards t2 
    ON t1.game_id=t2.game_id 
WHERE t2.player_id='$player_id' and 
     t1.date_of_game in (select MAX(date_of_game) 
          from games g join 
           scorecards ss 
           on g.game_id = ss.game_id and 
            ss.player_id = '$player_id' 
          where t1.course_id = g.course_id 
         ) 
GROUP BY t1.course_id 
ORDER BY t1.date_of_game DESC 

如果game_id是自動遞增,你可以用這個來代替date_of_game。如果兩場比賽可以在同一天進行同一場比賽,這一點尤爲重要。