2017-03-05 57 views
0

这是我的查询:MySQL的 - 选择最后一排为每个用户

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 

我得到这个:

result table

我不想重复行。只是让他们之间的谈话

+0

你能做的就是'订单DESC'你从'last到first'的顺序获得行,然后'LIMIT 1',你得到唯一的最后一行,即。第一行 – Alen

+0

您是否对表和列定义有严重问题?记录107和26具有相同的发送者。当有其他'FromUserId'时它不应该改变吗? – Whencesoever

+0

你为什么加入'tbl_forum_users'?你在哪里使用'u1'和'u2'? –

回答

2

快速修复可能会改变你的子查询

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的最后一行一起。

完整的查询:

​​

为了提高性能,你也应该将用户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 (...))中使用子查询。

此子查询也可以用来作为派生表,所以我们可以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])