【发布时间】:2012-08-06 08:01:34
【问题描述】:
我有下表
mes_id | user_a | user_b | message | room | time
------------------------------------------------------
1 | 23 | 24 | hello | 1 | 5676766767
user_a 在room 1 中为user_b 发送了message
我想获取每个房间的特定用户(例如 user_b)最近收到的所有消息,我尝试了几个查询,但没有得到正确的查询。
这个解决方案对我不起作用:sql_group_by_max
更新
我使用的是 5.5.18 MySQL 服务器
好的
这给了我结果谢谢
SELECT *
FROM messeges C
JOIN (
SELECT room, user_a, MAX( messeges.date ) AS max_time
FROM messeges
GROUP BY room, user_a
)a
ON a.room = c.room
AND a.user_a = c.user_a
AND a.max_time = c.date
WHERE c.user_b = '396'
【问题讨论】:
-
你能添加那几个查询吗?
-
请添加您尝试过的查询。我们更容易发现问题所在。
-
你使用的是什么数据库系统?