繁体   English   中英

Select 所有对话的最后一条消息(MySQL)

[英]Select last message from all conversations (MySQL)

我需要 select 为具有给定 ID 的用户的每个对话的所有最后消息。
如果最后一条消息被发送到给定的 id,它必须是来自发件人的最后一条消息。

这是没有使用 messageID 的 creationDate 的测试用例:

+-----------+------------+----------+------+
| messageID | fromUserID | toUserID | text |
+-----------+------------+----------+------+
| 1         | 1          | 2        | 'aa' |
| 2         | 1          | 3        | 'ab' |
| 3         | 2          | 1        | 'ac' |
| 4         | 2          | 1        | 'ad' |
| 5         | 3          | 2        | 'ae' |
+-----------+------------+----------+------+

userID=1 的结果必须是带有文本“ab”和“ad”的消息。

现在,我有这个查询,其中包含每个用户的所有最后一条消息,但根据我的测试用例,没有删除 id=1 的消息(必须只有 id=2 和 id=4)。

SELECT
    UM.messageID,
    UM.fromUserID, UM.toUserID,
    UM.text, UM.flags, UM.creationDate
FROM UserMessage AS UM
INNER JOIN
    (
        SELECT
            MAX(messageID) AS maxMessageID
        FROM UserMessage
        GROUP BY fromUserID, toUserID
    ) IUM
    ON UM.messageID = IUM.maxMessageID
WHERE UM.fromUserID = 1 OR UM.toUserID = 1
ORDER BY UM.messageID DESC

在此处输入图像描述

一个简单的方法是

select um.*
from usermessage um
where um.messageid = (select min(um2.messageid)
                      from usermessage um2
                      where (um2.fromuserid, touserid) in ( (um.fromuserid, um.touserid), (um.touserid, um.fromuserid) )
                     );

或者,在 MySQL 8+ 中:

select um.*
from (select um.*,
             row_number() over (partition by least(um.fromuserid, um.touserid), greatest(um.fromuserid, um.touserid) order by um.messageid desc) as seqnum
      from usermessage um
     ) um
where seqnum = 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM