我有以下數據的用戶表:MySQL查詢排名
+----+------------+--------------+--------------+--------------+ | id | name | points_game1 | points_game2 | points_game3 | +----+------------+--------------+--------------+--------------+ | 1 | player 1 | 10 | 0 | 5 | | 2 | player 2 | 0 | 25 | 0 | | 3 | player 3 | 30 | 3 | 0 | | 4 | player 4 | 0 | 0 | 20 | +----+------------+--------------+--------------+--------------+
我希望能夠表現出整體的行列,其中排名被定義爲總體最高得分,而不是排名每場比賽。所以在上面的例子中,我會用什麼查詢,顯示結果如下:
+----+------------+-------+----------------+ | id | name | score | game | +----+------------+-------+----------------+ | 3 | player 3 | 30 | points_game1 | | 2 | player 2 | 25 | points_game2 | | 4 | player 4 | 20 | points_game3 | | 1 | player 1 | 10 | points_game1 | +----+------------+-------+----------------+
你有沒有用過'UNPIVOT'看過? – Jodaka 2012-07-06 16:50:35
能否詳細說明一下? iv'e從未在mysql中使用過這種方法 – 2012-07-06 21:14:35
'UNPIVOT'讓我們列出列名並使其成爲行的一部分:例如,每個玩家最終會得到三行,每行有四列:id,name,game ,分。除了我剛纔意識到你在使用MySQL,並且MySQL中不支持「UNPIVOT」。抱歉。 http://forums.mysql.com/read.php?10,379790有一個好的解決辦法。 – Jodaka 2012-07-10 01:28:50