Mysql消息传递应用程序查询最新的一对一消息顺序

时间:2017-03-21 09:12:18

标签: mysql

表格结构

    CREATE TABLE IF NOT EXISTS `mail_box` (
      `msg_id` int(11) NOT NULL AUTO_INCREMENT,
      `sender_id` int(11) NOT NULL,
      `receiver_id` int(11) NOT NULL,
      `message` text NOT NULL,
      `date` timestamp NOT NULL,
      `attachment` varchar(255) NOT NULL,
      `status` tinyint(1) NOT NULL,
      PRIMARY KEY (`msg_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL,
    `name` varchar(255) NOT NULL
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

需要查询一对一会话

查询我尝试按最新的

进行一对一的订阅
SELECT usr.name as Receiver,usr1.name as Sender, message,date
FROM mail_box 
JOIN users as usr on usr.id = receiver_id 
JOIN users as usr1 on usr1.id = sender_id 
AND receiver_id IN (1,2)
AND sender_id IN (1,2)
ORDER BY date DESC

此查询正常运行。是否有任何替代查询相同的结果。

0 个答案:

没有答案