我有一张桌子:
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和另一个用户之间的所有消息组中选择最后一条消息
——我怎么能做到这一点?