显示每个对话的最新消息以及是否回复

时间:2018-10-23 18:41:10

标签: mysql

我正在开发一个消息传递系统,并试图以正确的方式获取数据,但是却被卡住了。

#Message Table

CREATE TABLE `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `subject` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

INSERT INTO `message` (`id`, `subject`)
VALUES
    (1, 'Test'),
    (2, 'Test Again'),
    (6, 'Nice!'),
    (7, 'Next');

#Message User Table

CREATE TABLE `message_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `interlocutor` int(11) DEFAULT NULL,
  `body` text,
  `folder` enum('inbox','sent') NOT NULL,
  `starmark` tinyint(1) NOT NULL DEFAULT '0',
  `unread` tinyint(1) NOT NULL DEFAULT '1',
  `deleted` enum('none','trash','deleted') NOT NULL DEFAULT 'none',
  `created_date` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;

INSERT INTO `message_user` (`id`, `message_id`, `user_id`, `interlocutor`, `body`, `folder`, `starmark`, `unread`, `deleted`, `created_date`)
VALUES
    (1, 1, 1, 2, 'Hi, how are you?', 'sent', 0, 1, 'none', '2018-10-23 09:36:02'),
    (2, 1, 2, 1, 'Hi, how are you?', 'inbox', 0, 1, 'none', '2018-10-23 09:36:02'),
    (3, 1, 2, 1, 'I am good thanks, you?', 'sent', 0, 1, 'none', '2018-10-23 09:46:02'),
    (4, 1, 1, 2, 'I am good thanks, you?', 'inbox', 0, 1, 'none', '2018-10-23 09:46:02'),
    (13, 6, 1, 2, 'Hi how are you', 'sent', 0, 0, 'none', '2018-10-23 15:40:43'),
    (14, 6, 2, 1, 'Hi how are you', 'inbox', 0, 1, 'none', '2018-10-23 15:40:43'),
    (16, 6, 1, 2, 'Also, what time is it at yours?', 'sent', 0, 0, 'none', '2018-10-23 15:44:20'),
    (17, 6, 2, 1, 'Also, what time is it at yours?', 'inbox', 0, 1, 'none', '2018-10-23 15:44:20'),
    (18, 6, 2, 1, 'Hey and 18:44', 'sent', 0, 0, 'none', '2018-10-23 15:44:54'),
    (19, 6, 1, 2, 'Hey and 18:44', 'inbox', 0, 1, 'none', '2018-10-23 15:44:56'),
    (20, 6, 2, 1, 'You ok?', 'sent', 0, 0, 'none', '2018-10-23 15:44:56'),
    (21, 6, 1, 2, 'You ok?', 'inbox', 0, 1, 'none', '2018-10-23 15:44:54'),
    (22, 2, 2, 1, 'Hey', 'inbox', 0, 1, 'none', '2018-10-23 19:20:23'),
    (23, 2, 1, 2, 'Hey', 'sent', 0, 1, 'none', '2018-10-23 19:20:23'),
    (24, 7, 2, 1, 'Hello', 'sent', 0, 1, 'none', '2018-10-23 19:29:36'),
    (25, 7, 1, 2, 'Hello', 'inbox', 0, 1, 'none', '2018-10-23 19:29:55'),
    (26, 7, 1, 2, 'Hey!', 'sent', 0, 1, 'none', '2018-10-23 19:30:30'),
    (27, 7, 2, 1, 'Hey!', 'inbox', 0, 1, 'none', '2018-10-23 19:30:43');

我正在尝试获取有关用户收件箱(ID 1)的消息。我想在未删除的地方显示收到的最新消息。除此之外,我还要有一个标志,指示我是否已回复该邮件。

上面的数据显示有4条消息。

消息1显示了用户1发送消息并获得响应的示例。身体应该显示“我很好,谢谢?”来自ID4。应为0。

消息2不应该显示,因为我给他们发送了一条消息,他们还没有回复。

消息6显示了混合对话的示例。他们对我的最后一次回应两次。身体应该显示“你还好吗?”来自ID 21。

消息7显示了我已答复的消息示例。正文应显示ID 25中的“ Hello”。由于ID 26而应显示为1。

我的尝试

SELECT  dt.*
FROM (
      SELECT  m.id,
              m.subject,
              mu.id AS message_user_id,
              mu.message_id,
              mu.user_id,
              mu.interlocutor,
              mu.body,
              mu.folder,
              mu.starmark,
              mu.unread,
              mu.deleted,
              mu.created_date,
              Row_number()
                OVER (PARTITION BY m.id 
                      ORDER BY field(mu.folder, 'sent', 'inbox') DESC) AS row_no
        FROM  message m
        JOIN  message_user mu
          ON  m.id = mu.message_id
        WHERE mu.deleted = 'none'
          AND mu.user_id = 1
          AND folder = 'inbox') AS dt
WHERE    dt.row_no = 1
ORDER BY dt.id DESC;

此处未发出标记,并且消息ID 6显示响应ID 19而不是ID 21,因为它们两次答复。

0 个答案:

没有答案