我的表格结构如下:
我正在尝试为每个玩家选择最新的盘口,因此按日期和玩家 ID 排序。
考虑以下查询:
SELECT h.id, h.playerId, h.amount, h.date
FROM hancicap h
WHERE h.date < '2012-06-11'
ORDER BY h.playerId ASC, h.date DESC
这会产生如下结果:
(带下划线的行是我想要返回的行)
我的第一个想法是添加一个GROUP BY h.playerId
,但这会导致错误的值。我完全陷入困境,非常感谢所有建议和想法!
不确定我应该给该线程命名什么。进行编辑以使名称更正确就好了。
最佳答案
您可以使用子查询来过滤掉最新的行:
select *
from players p1
where p1.id =
(
select id
from players p2
where p2.playerId = p1.playerId
order by
date desc
limit 1
)
关于mysql - 返回查询中每个 ID 的第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11010373/