2014-10-28 69 views
0

我試圖在視頻遊戲作弊和提示頁面的索引中顯示所有可用作弊的列表,但我通過視頻遊戲將結果分組並計算了多少欺騙視頻遊戲,這個我可以完成,但我試圖展示最後加入的作弊遊戲。MySQL通過查詢獲取組上的最後附加值的名稱

我的查詢是:

SELECT a_games.game_id, COUNT(*) AS cheat_count, a_games.game_fname, a_games.game_logo, a_cheats.cheat_title FROM a_cheats 
       LEFT JOIN a_games ON a_games.game_id=a_cheats.game_id 
       GROUP BY a_cheats.game_id 

這隻能說明第一個加入的騙子。

我試着在cheat_id上使用max,但值cheat_title不斷顯示第一次添加的作弊。

表a_cheats

cheat_id type_id member_id game_id cheat_title cheat_body cheat_date  
1  | 1  | 1   | 22  | Truques V...| Introduz...| 2014-10-...| 
2  | 1  | 1   | 25  | Invulnera...| Durante ...| 2014-10-...| 
3  | 1  | 1   | 25  | Modo Debu...| Durante ...| 2014-10-...|  
4  | 1  | 1   | 25  | Charme In...| Durante ...| 2014-10-...| 
5  | 1  | 1   | 36  | Cabeças e...| Começa o...| 2014-10-...| 

表a_games

game_id genre_id member_id game_fname game_sname game_logo 
22  | 15  | 1   | 4x4 Worl...|    | 5259da0... 
25  | 3  | 1   | Akuji th...|    | 5287ae0... 
36  | 25  | 1   | All Star...|    | 5287daa... 

所以在結果Akuji無心應顯示cheat_title 「查姆Infinito的」,這是那場比賽

最後添加備忘查詢結果

game_id game_count game_fname   game_logo        cheat_title 
22  | 1   | 4x4 World Trophy | 5259da0527128_ava_4x4worldtrophy.jpg | Truques (Vários) 
25  | 3   | Akuji The Heartless | 5287ae093e115_ava_akujiheartless.jpg | Invulnerabilidade 
36  | 1   | All Star Tennis'99 | 5287daa2695ef_ava_allstartennis99.jpg| Cabeças e pés grandes 
+1

你試過'ORDER BY a_games.game_id,a_cheats.cheat_id DESC'? 也許你需要顯示你的數據庫模式。 – 2014-10-28 05:18:21

+0

試過了,不起作用。我也嘗試在cheats_id,cheats_date上使用max(),但只有那些特定字段獲取最後一個條目,而cheats_title在看到您的模式後繼續給出第一個條目 – 2014-10-28 05:55:37

+0

,則可以使用'ORDER BY a_games.game_id,a_cheats.cheat_date DESC' – 2014-10-28 06:18:38

回答

0

看起來一旦你打電話給count(),你無法控制後面選擇哪個title(它直接獲得最早條目的標題)。因此,我們可以通過稍後調用計數來執行您想要的操作。

注意:如果您希望它更可靠,您可以將ORDER BY a_cheats.cheat_id DESC更改爲ORDER BY a_cheats.cheat_date DESC

SELECT gameID,gameName,cheatTitle, COUNT(gameID) AS cheat_count 
FROM (
    SELECT a_games.game_id as gameID, a_games.game_fname as gameName, a_cheats.cheat_title as cheatTitle 
    FROM a_cheats 
    JOIN a_games ON a_games.game_id=a_cheats.game_id 
    ORDER BY a_cheats.cheat_id DESC 
) ungrouped 
GROUP BY gameID 

SQLFiddle:http://sqlfiddle.com/#!2/63f888/18

OR

SELECT gameID,gameName,cheatTitle, COUNT(cheatID) AS cheat_count 
FROM (
    SELECT a_games.game_id as gameID, a_games.game_fname as gameName, a_cheats.cheat_title as cheatTitle, a_cheats.cheat_id as cheatID 
    FROM a_cheats 
    RIGHT JOIN a_games ON a_games.game_id=a_cheats.game_id 
    ORDER BY a_cheats.cheat_id DESC 
) ungrouped 
GROUP BY gameID 

SQLFiddle:http://sqlfiddle.com/#!2/63f888/19

+0

謝謝,工作起來很有魅力, t知道sqlfiddle。方便工具。再次感謝 – 2014-10-28 07:29:31

+0

太棒了!沒問題 – 2014-10-28 07:30:36

相關問題