【发布时间】:2013-08-22 03:22:18
【问题描述】:
在我的 MySQL 数据库中,我有一个这样的表,用于存储来自任何人的对话消息
id int(11) id of the message
from member_id int(11) id of the person the message was sent from
to member_id int(11) id of the person the message was sent to
date sent datetime date of when it was sent
active tinyint(1) if the message is deleted
text longtext the text of the message
from_read tinyint(1) boolean to know if the person who sent it read it
to_read tinyint(1) boolean to know if the person who it got sent to read it
例如,它可能是这样的:
from_member_id to_member_id date sent
1 2 june 12
1 3 june 13
2 3 june 14
3 1 june 9
所以我们在1和2、1和3、2和3之间进行了对话。
我正在尝试获取一个 select 语句,它将从用户参与的每个对话中为我提供当前用户参与的最新消息。因此,如果 1 已登录,那么我希望得到 2 行.结果集中的第一行将是上面的第二行(7 月 13 日),因为它是最新的,然后结果集中的第二行将是上面的第一行(6 月 12 日),这是来自 @ 的最新987654330@的两次对话。结果集还需要按发送日期排序,因此较新的对话列在顶部。
我正在尝试做的就像在安卓手机中发短信一样,您可以在其中看到对话列表以及每个列表中的最新消息。
这是我的 sql 查询
SELECT *
FROM (
SELECT *
FROM message
WHERE `from member_id`=1 OR `to member_id`=1
ORDER BY IF(`from member_id`=1, `to member_id`, `from member_id`)
) as t
GROUP BY IF(`from member_id`=1, `to member_id`, `from member_id`)
我只是将1 硬编码为当前用户。我正在做的是,按我可以使用 if 语句检查的另一个人的 id 对它们进行排序,然后对该结果进行分组,以便我尝试从每个对话中获取最近的一个。
问题是分组时,每个组可以有超过 1 行,它似乎只是随机选择一些行。如何让它选择具有最近发送日期值的行?
【问题讨论】:
标签: mysql select greatest-n-per-group