SQL选择最后一条消息

时间:2012-06-14 11:52:29

标签: sql optimization query-optimization

我有一张桌子:

CREATE TABLE `tMessages` (
 `tMessages_id` bigint(20) NOT NULL AUTO_INCREMENT,
 `tUsers_sender_uid` bigint(20) NOT NULL,
 `tUsers_recipient_uid` bigint(20) NOT NULL,
 `tMessages_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `tMessages_text` varchar(2000) NOT NULL,
 PRIMARY KEY (`tMessages_id`),
 KEY `tUsers_sender_uid` (`tUsers_sender_uid`),
 KEY `tUsers_recipient_uid` (`tUsers_recipient_uid`),
 KEY `tUsers_uids` (`tUsers_sender_uid`,`tUsers_recipient_uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

我需要:

1)选择两个用户之间的所有消息,我这样做了:

SELECT * FROM `tMessages` FORCE INDEX (tUsers_uids) WHERE tUsers_sender_uid = 1 AND tUsers_recipient_uid=2 OR tUsers_sender_uid=2 AND tUsers_recipient_uid = 1

- 这是一种快速方法吗?

2)选择uid = 1和其他用户之间的所有消息组中的最后一条消息

- 我怎么能这样做?

5 个答案:

答案 0 :(得分:0)

SELECT m.*  
  FROM tMessages m
  WHERE (tUsers_sender_uid = 1 AND tUsers_recipient_uid=2 OR tUsers_sender_uid=2 AND tUsers_recipient_uid = 1)
    and tMessages_timestamp = (select max(tMessages_timestamp) 
                                 from tMessages 
                                 WHERE (tUsers_sender_uid = 1 AND tUsers_recipient_uid=2 OR tUsers_sender_uid=2 AND tUsers_recipient_uid = 1));

答案 1 :(得分:0)

试试这个

select top(1)* from tMessages WHERE (tUsers_sender_uid = 1
AND tUsers_recipient_uid=2) OR (tUsers_sender_uid=2 AND 
tUsers_recipient_uid = 1) order by tMessages_timestamp desc

答案 2 :(得分:0)

这个将返回任意2个用户之间的所有最后消息:

SELECT m.*  
  FROM tMessages m
    where tMessages_timestamp = (select max(tMessages_timestamp) 
                                   from tMessages 
                                   WHERE (tUsers_sender_uid = m.tUsers_sender_uid    AND tUsers_recipient_uid = m.tUsers_recipient_uid OR 
                                          tUsers_sender_uid = m.tUsers_recipient_uid AND tUsers_recipient_uid = m.tUsers_sender_uid));

答案 3 :(得分:0)

这个将返回某个用户(即1)和任何其他用户之间的所有最后消息:

SELECT m.*  
  FROM tMessages m
    where m.tMessages_timestamp = (select max(tMessages_timestamp) 
                                   from tMessages 
                                   WHERE (tUsers_sender_uid = 1                      AND tUsers_recipient_uid = m.tUsers_recipient_uid OR 
                                          tUsers_sender_uid = m.tUsers_recipient_uid AND tUsers_recipient_uid = 1))
      and (m.tUsers_sender_uid = 1 or m.tUsers_recipient_uid = 1);

答案 4 :(得分:0)

这是第一部分......需要括号吗?

SELECT * FROM `tMessages`
WHERE (tUsers_sender_uid = 1 AND tUsers_recipient_uid=2)
 OR (tUsers_sender_uid=2 AND tUsers_recipient_uid = 1)

第二部分可能会更具挑战性......但这应该让你走上正轨。

select coalesce(r.recipient, s.sender) OtherParty, 
    case when coalesce(r.`tMessages_id`,s.`tMessages_id`) > coalesce(s.`tMessages_id`,r.`tMessages_id`)
        then coalesce(r.`tMessages_id`,s.`tMessages_id`)
        else coalesce(s.`tMessages_id`,r.`tMessages_id`)
    end messageid
from
    (select `tUsers_recipient_uid` recipient, max(`tMessages_id`) `tMessages_id` 
    from `tMessages`
    where `tUsers_sender_uid` = 1
    group by `tUsers_recipient_uid`) r
full outer join (
    select `tUsers_sender_uid` sender, max(`tMessages_id`) `tMessages_id`
    from `tMessages`
    where `tUsers_recipient_uid`= 1
    group by `tUsers_sender_uid`) s
on r.recipient = s.sender