我很高兴通过在PHP中构建使用API发送和接收消息的自定义消息传递系统来重新创建手机。 我正试图在桌面网站上模仿Facebook消息中的功能。
[Col 1] [Col 2]
A list of the Conversation View.
latest messages
received in order
of Newest to oldest
我目前在MySQL中有一个表,结构如下:
CREATE TABLE IF NOT EXISTS `History` (
`ID` int(10) NOT NULL AUTO_INCREMENT COMMENT 'MessageID',
`Sender` varchar(10) NOT NULL,
`Recipient` varchar(10) NOT NULL,
`ExtReference` int(20) DEFAULT NULL,
`Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Status` varchar(100) NOT NULL,
`userid` int(3) NOT NULL,
`Message` longtext NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=609 ;
将样本日期设置为:
INSERT INTO `History` (`ID`, `Sender`, `Recipient`, `ExtReference`, `Date`, `Status`, `userid`, `Message`) VALUES
(1, '0412345678', '0468888888', 33845909, '2013-03-17 04:17:34', '1', 11, 'Just testing....'),
(2, '0412345678', '0400222333', 33845910, '2013-03-17 04:17:35', '1', 11, 'Amazing'),
(3, '0412345678', '0411111111', 33847419, '2013-03-17 04:46:04', '1', 8, 'Nothing here to see'),
(4, '0412345678', '0400222333', 33850155, '2013-03-17 06:31:57', '1', 12, 'Hello there Mr IT Guru :-)'),
(5, '0400222333', '0412345678', 33850179, '2013-03-17 06:33:21', '1', 12, '[Write message here]'),
(6, '0412345678', '0411111111', 33955423, '2013-03-23 01:26:22', '1', 8, 'Hello Charles'),
(7, '0412345678', '0411111111', 33959071, '2013-03-23 03:08:26', '1', 13, 'Sample Message'),
(8, '0400222333', '0412345678', 33964111, '2013-03-23 05:27:51', '1', 13, 'How do I use this system?'),
(9, '0400222333', '0412345678', 34107503, '2013-03-30 03:13:38', '1', 12, 'Is this thing on?'),
(10, '0412345678', '0401411210', 34230869, '2013-03-05 00:18:09', '1', 16, 'Hello')
(在这个例子中,我的号码是:0412345678)。 SQL小提琴:http://sqlfiddle.com/#!2/29197/1/0
我已经找到了如何获取发件人和收件人列中使用的所有唯一数字的列表:
SELECT DISTINCT `Sender` AS phoneID FROM `History`
UNION
SELECT DISTINCT `Recipient` AS phoneID FROM `History`
但我无法解决如何将最新的日期和消息附加到此数据。
如果我专注于发送给我或发送给我的消息,我可以使用这两个消息:
SELECT `ID`, `Sender`, `Recipient`, MAX(`Date`), `Message` FROM History
GROUP BY Sender
ORDER BY `History`.`Date` DESC
或
SELECT `ID`, `Sender`, `Recipient`, MAX(`Date`), `Message`, `Status` FROM History
GROUP BY Recipient
ORDER BY `History`.`Date` DESC
有什么想法? 如果需要,我可以重新创建历史表格。
我还需要尝试将电话号码与稍后在轨道上的联系人表格中的人名一起加入。
由于 查理
答案 0 :(得分:0)
可能不是最好的方法,但你可以将你的两个查询结合起来。类似的东西:
SELECT `ID`, `Sender`, `Recipient`,`Date`,`Message`,`Status` FROM
(
SELECT `ID`, `Sender`, `Recipient`, `Date`, `Message`,`Status` FROM History
WHERE Sender = "0412345678"
GROUP BY Sender
UNION
SELECT `ID`, `Sender`, `Recipient`, MAX(`Date`), `Message`, `Status` FROM History
WHERE Recipient = "0412345678"
GROUP BY Recipient
) res
GROUP BY res.ID
ORDER BY res.Date DESC
请注意,这是针对特定数字的。如果不需要,您可以删除WHERE
条款。