我有一个这样的表结构:

ID  Win     Bet    UserID       Date
1   win     1000     1      2014-11-12 19:15:34
2   win     1000     1      2014-11-12 19:15:49
3   loose   1000     1      2014-11-12 19:21:52


我运行这样的查询:

SELECT
    u.Username,
    SUM(b.Bet) AS totalWin,
    b.date,
    b.win
FROM blackjack_history AS b
INNER JOIN users_main AS u ON b.UserID = u.ID
WHERE b.win = 'win' GROUP BY b.UserID ORDER BY totalWin DESC, LIMIT 10


当我运行此查询时,Probem会返回与日期有关的ID 1行。
我该如何返回ID 2的日期?我尝试使用Order By,但这似乎无法完成工作

最佳答案

您不想要最新的记录。您也在进行聚合。因此,只需使用max()

SELECT u.Username, SUM(b.Bet) AS totalWin, max(b.date) as date, b.win
FROM blackjack_history b INNER JOIN
     users_main u
     ON b.UserID = u.ID
WHERE b.win = 'win'
GROUP BY b.UserID
ORDER BY totalWin DESC
LIMIT 10;

关于mysql - 获取有关分组依据的最新记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26895003/

10-13 09:02