表格结构
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
此查询正常运行。是否有任何替代查询相同的结果。