Mysql - select 每个用户的最后一行

Mysql - select last row for each user

这是我的查询:

 SELECT msgId as `ID`,msgFromUserId , msgToUserId ,  
            DATE_FORMAT( msgDate,'%d/%m/%y %H:%i') as `time` ,  MID(msgText,1,30) as `text` , 
            (CASE WHEN (msgFromUserId=292646) then  b.user_login else  a.user_login END) as `sender`
            FROM tbl_messages inner join wp_users as a on tbl_messages.msgFromUserId=a.ID 
            inner join wp_users as b on tbl_messages.msgToUserId=b.ID 
            inner join tbl_forum_users u1 on tbl_messages.msgFromUserId=u1.user_ID 
            inner join tbl_forum_users u2 on tbl_messages.msgToUserId=u2.user_ID 
where (msgFromUserId=292646 or msgToUserId=292646)
and tbl_messages.msgId in (SELECT max(msgId) FROM tbl_messages   GROUP BY msgFromUserId, msgToUserId )
order by msgId desc

我明白了:

我不想要重复的行。只看他们之间的最后一句对话

快速修复可能是更改您的子查询

SELECT max(msgId) FROM tbl_messages   GROUP BY msgFromUserId, msgToUserId

SELECT max(msgId) 
FROM tbl_messages
GROUP BY LEAST(msgFromUserId, msgToUserId), GREATEST(msgFromUserId, msgToUserId)

这会将 292646 到 1 和 1 到 292646 的消息分组在一起。

完成查询:

SELECT msgId as `ID`,msgFromUserId , msgToUserId ,  
            DATE_FORMAT( msgDate,'%d/%m/%y %H:%i') as `time` ,  MID(msgText,1,30) as `text` , 
            (CASE WHEN (msgFromUserId=292646) then  b.user_login else  a.user_login END) as `sender`
            FROM tbl_messages inner join wp_users as a on tbl_messages.msgFromUserId=a.ID 
            inner join wp_users as b on tbl_messages.msgToUserId=b.ID 
            inner join tbl_forum_users u1 on tbl_messages.msgFromUserId=u1.user_ID 
            inner join tbl_forum_users u2 on tbl_messages.msgToUserId=u2.user_ID 
where (msgFromUserId=292646 or msgToUserId=292646)
and tbl_messages.msgId in (
    SELECT max(msgId) 
    FROM tbl_messages
    GROUP BY LEAST(msgFromUserId, msgToUserId), GREATEST(msgFromUserId, msgToUserId)
)
order by msgId desc

为了提高性能,您还应该将用户 ID 条件移动到子查询中:

SELECT max(msgId) 
FROM tbl_messages
where (msgFromUserId=292646 or msgToUserId=292646) -- <-- here
GROUP BY LEAST(msgFromUserId, msgToUserId), GREATEST(msgFromUserId, msgToUserId)

要以最佳方式使用索引,您应该使用 UNION ALL 优化。但这看起来会很复杂:

SELECT max(msgId)
FROM (
    SELECT msgToUserId as otherUserId, max(msgId) as msgId
    FROM tbl_messages 
    WHERE msgFromUserId=292646 
    GROUP BY msgToUserId

    UNION ALL

    SELECT msgFromUserId as otherUserId, max(msgId) as msgId
    FROM tbl_messages 
    WHERE msgToUserId=292646 
    GROUP BY msgFromUserId
) sub
GROUP BY otherUserId

请注意,这只是 WHERE 子句中使用的子查询 (tbl_messages.msgId in (...))。

这个子查询也可以作为派生的table,所以我们可以用tbl_messages加入它:

SELECT msgId as `ID`,
       msgFromUserId,
       msgToUserId,  
       DATE_FORMAT( msgDate,'%d/%m/%y %H:%i') as `time`,
       MID(msgText,1,30) as `text` , 
       (CASE WHEN (msgFromUserId=292646) then  b.user_login else  a.user_login END) as `sender`
FROM (
    SELECT max(msgId) as msgId
    FROM (
        SELECT msgToUserId as otherUserId, max(msgId) as msgId
        FROM tbl_messages 
        WHERE msgFromUserId=292646 
        GROUP BY msgToUserId
        UNION ALL
        SELECT msgFromUserId as otherUserId, max(msgId) as msgId
        FROM tbl_messages 
        WHERE msgToUserId=292646 
        GROUP BY msgFromUserId
    ) sub
    GROUP BY otherUserId
) sub
inner join tbl_messages on tbl_messages.msgId = sub.msgId
inner join wp_users as a on tbl_messages.msgFromUserId=a.ID 
inner join wp_users as b on tbl_messages.msgToUserId=b.ID 
inner join tbl_forum_users u1 on tbl_messages.msgFromUserId=u1.user_ID 
inner join tbl_forum_users u2 on tbl_messages.msgToUserId=u2.user_ID 
order by tbl_messages.msgId desc

您需要以下索引来支持子查询:

tbl_messages(msgFromUserId, msgToUserId [, msgId])
tbl_messages(msgToUserId, msgFromUserId [, msgId])