2

我正在尝试实现一个与 facebook 非常相似的消息系统。消息表是:

+--------+----------+--------+-----+----------+
| msg_id | msg_from | msg_to | msg | msg_time |
+--------+----------+--------+-----+----------+

这里包含用户 ID 和msg_from包含消息的时间戳。一个用户的用户 ID 可以同时出现在 to 和 from 列中,并且对于另一个用户可以多次出现。我应该如何编写一个 SQL 查询来选择两个用户之间最近发送的消息?(消息可以来自任何一个) 1 to 2 或 2 to 1 。msg_tomsg_time

4

4 回答 4

4

由于 John Woo 澄清它不是定向的,这是我的新答案:

select *
from msgsList
where (least(msg_from, msg_to), greatest(msg_from, msg_to), msg_time)       
in 
(
    select 
       least(msg_from, msg_to) as x, greatest(msg_from, msg_to) as y, 
       max(msg_time) as msg_time
    from msgsList 
    group by x, y
);

输出:

| MSG_ID | MSG_FROM | MSG_TO |    MSG |                       MSG_TIME |
------------------------------------------------------------------------
|      1 |        1 |      2 |  hello | January, 23 2010 17:00:00-0800 |
|      5 |        1 |      3 | me too | January, 23 2012 00:15:00-0800 |
|      6 |        3 |      2 |  hello | January, 23 2012 01:12:12-0800 |

对于此输入:

create table msgsList
(
  msg_id int,
  msg_from int, 
  msg_to int,
  msg varchar(10),
  msg_time datetime
);

insert into msgslist VALUES

(1, 1, 2, 'hello', '2010-01-23 17:00:00'),      -- shown
(2, 2, 1, 'world', '2010-01-23 16:00:00'),

(3, 3, 1, 'i am alive', '2011-01-23 00:00:00'),
(4, 3, 1, 'really', '2011-01-22 23:15:00'),
(5, 1, 3, 'me too', '2012-01-23 00:15:00'),     -- shown

(6, 3, 2, 'hello', '2012-01-23 01:12:12');      -- shown

SQLFiddle 演示


如果 ANSI SQL 是您的一杯茶,那么可以这样做:http ://sqlfiddle.com/#!2/0a575/19

select *
from msgsList z
where exists
(
    select null
    from msgsList
    where 
      least(z.msg_from, z.msg_to) = least(msg_from, msg_to)
      and greatest(z.msg_from, z.msg_to) = greatest(msg_from, msg_to)
    group by least(msg_from, msg_to), greatest(msg_from, msg_to)
    having max(msg_time) = z.msg_time  
) ;
于 2012-08-19T15:46:52.377 回答
2

会不会这么简单?http://www.sqlfiddle.com/#!2/50f9f/1

set @User1 := 'John';
set @User2 := 'Paul';


select *
from
(
  select *
  from messages 
  where msg_from = @User1 and msg_to = @User2
  order by msg_time desc
  limit 1
) as x
union
select *
from
(
  select *
  from messages 
  where msg_from = @User2 and msg_to = @User1
  order by msg_time desc
  limit 1
) as x
order by msg_time desc

输出:

| MSG_ID | MSG_FROM | MSG_TO |         MSG |                      MSG_TIME |
----------------------------------------------------------------------------
|      2 |     Paul |   John | Hey Johnny! | August, 20 2012 00:00:00-0700 |
|      1 |     John |   Paul | Hey Paulie! | August, 19 2012 00:00:00-0700 |

如果只有 MySQL 支持窗口功能,可能会简单得多:http ://www.sqlfiddle.com/#!1/e4781/8

with recent_message as
(
select *, rank() over(partition by msg_from, msg_to order by msg_time desc) as r
from messages
)
select * 
from recent_message 
where r = 1 
    and 
    (
      (msg_from = 'John' and msg_to = 'Paul') 
      or
      (msg_from = 'Paul' and msg_to = 'John')
    )
order by msg_time desc;
于 2012-08-19T15:01:01.977 回答
1

对于像这样的任何复杂查询,请使用 TDQD — 测试驱动的查询设计。逐步设计答案,步骤的大小取决于您的经验以及您对问题的理解程度。

第 1 步 — 查找给定用户之间最新消息的时间

在整个过程中,我假设用户 ID 是整数。我正在使用值 1000 和 2000。

SELECT MAX(msg_time) AS msg_time
  FROM message
 WHERE ((msg_to = 1000 AND msg_from = 2000) OR
        (msg_to = 2000 AND msg_from = 1000)
       )

第 2 步 — 查找与最新消息对应的记录

SELECT m.*
  FROM message AS m
  JOIN (SELECT MAX(msg_time) AS msg_time
          FROM message
         WHERE ((msg_to = 1000 AND msg_from = 2000) OR
                (msg_to = 2000 AND msg_from = 1000)
               )
       ) AS t
    ON t.msg_time = m.msg_time
 WHERE ((m.msg_to = 1000 AND m.msg_from = 2000) OR
        (m.msg_to = 2000 AND m.msg_from = 1000)
       )

如果这些字符之间恰好有两个(或更多)消息具有相同的最新时间戳,那么它们都会被选中;目前没有在碰撞之间进行选择的依据。如果您认为这是一个问题,您可以安排MAX(msg_id)使用上面的查询(作为子查询)来查找:

SELECT m2.*
  FROM message AS m2
  JOIN (SELECT MAX(m.msg_id) AS msg_id
          FROM message AS m
          JOIN (SELECT MAX(msg_time) AS msg_time
                  FROM message
                 WHERE ((msg_to = 1000 AND msg_from = 2000) OR
                        (msg_to = 2000 AND msg_from = 1000)
                       )
               ) AS t
            ON t.msg_time = m.msg_time
         WHERE ((m.msg_to = 1000 AND m.msg_from = 2000) OR
                (m.msg_to = 2000 AND m.msg_from = 1000)
               )
       ) AS i
    ON i.msg_id = m2.msg_id

警告代码未经任何 DBMS 正式测试。

于 2012-08-19T15:47:53.500 回答
0

经过一番思考,我想出了这个:

SELECT min_user AS min(msg_from, msg_to), max_user AS max(msg_from, msg_to), 
max(msg_date) FROM msg GROUP BY min_user, max_user

我仍然不太确定如何从消息中获取附加数据,但我会考虑一下。

于 2012-08-19T14:16:39.620 回答