我有以下MySQL表设置:SQL Fiddle SQL Fiddle #2 - 我想要实现的是为每个单独的号码选择最新的消息。 From和To始终是这样或那样的,取决于消息的方向。此表中可能列出了多个数字。
感谢。
messages_outgoing
+----+----------------+----------------+--------------------------+-----------+---------------------+
| ID | TO | FROM | BODY | SMSSTATUS | DATE |
+----+----------------+----------------+--------------------------+-----------+---------------------+
| 1 | +4407790000000 | +4407790000001 | Outgoing Message | pending | 2014-07-21 17:10:00 |
| 2 | +4407790000000 | +4407790000001 | Another outgoing message | pending | 2014-07-21 17:20:00 |
| 3 | +4407790000000 | +4407790000001 | Final message sent | pending | 2014-07-21 17:30:00 |
| 4 | +4407790000003 | +4407790000001 | Outgoing Message | pending | 2014-07-21 17:30:00 |
| 5 | +4407790000004 | +4407790000001 | Another message out | pending | 2014-07-21 17:30:00 |
| 6 | +4407790000005 | +4407790000001 | Information Message | pending | 2014-07-21 17:40:00 |
+----+----------------+----------------+--------------------------+-----------+---------------------+
messages_incoming
+----+----------------+----------------+--------------------+---------------------------------+
| ID | TO | FROM | BODY | SMSSTATUS | DATE |
+----+----------------+----------------+--------------------+---------------------------------+
| 1 | +4407790000001 | +4407790000000 | Replying to first | received | 2014-07-21 17:15:00 |
| 2 | +4407790000001 | +4407790000000 | Replying to second | received | 2014-07-21 17:25:00 |
| 3 | +4407790000001 | +4407790000005 | OK, Received | received | 2014-07-21 17:45:00 |
+----+----------------+----------------+--------------------+-----------+---------------------+
最终结果
+----+----------------+----------------+--------------------------+-----------+---------------------+----------+
| id | To | From | Body | SmsStatus | date | source |
+----+----------------+----------------+--------------------------+-----------+---------------------+----------+
| 3 | +4407790000000 | +4407790000001 | Final message sent | pending | 2014-07-21 17:30:00 | outgoing |
| 4 | +4407790000003 | +4407790000001 | Outgoing Message | pending | 2014-07-21 17:30:00 | outgoing |
| 5 | +4407790000004 | +4407790000001 | Another message out | pending | 2014-07-21 17:30:00 | outgoing |
| 3 | +4407790000001 | +4407790000005 | OK, Received | received | 2014-07-21 17:45:00 | incoming |
+----+----------------+----------------+--------------------------+-----------+---------------------+----------+
+4407790000000与+4407790000001之间的对话 - 最新消息为“发送最终消息”
+4407790000003与+4407790000001之间的对话 - 最新消息为“外发留言”
+4407790000004和+4407790000001之间的对话 - 最新消息将是“另一条消息”
+4407790000005和+4407790000001之间的对话 - 最新消息为“OK,Received”
可能的解决方案
我提出了以下SQL查询。老实说,我认为这是一个非常糟糕的查询,它实际上是有效的并且返回我正在追求的内容。
SELECT `SmsStatus` , `Body` , `all_messages`.`Number` , `all_messages`.`date`
FROM (
SELECT `SmsStatus` , `Body` , `To` as `Number` , `date` FROM `messages_outgoing`
UNION
SELECT `SmsStatus` , `Body` , `From` as `Number` , `date` FROM `messages_incoming`
) AS `messages`
INNER JOIN
(
SELECT `messages`.`Number` , MAX(`date`) as `date`
FROM (
SELECT `SmsStatus` , `Body` , `To` as `Number` , `date` FROM `messages_outgoing`
UNION
SELECT `SmsStatus` , `Body` , `From` as `Number` , `date` FROM `messages_incoming`
) AS `messages` GROUP BY `Number`
) AS `all_messages`
ON `all_messages`.`Number` = `messages`.`Number` AND `all_messages`.`date` = `messages`.`date`
结果
+-----------+---------------------+----------------+---------------------+
| SMSSTATUS | BODY | NUMBER | DATE |
+-----------+---------------------+----------------+---------------------+
| pending | Final message sent | +4407790000000 | 2014-07-21 17:30:00 |
| pending | Outgoing Message | +4407790000003 | 2014-07-21 17:30:00 |
| pending | Another message out | +4407790000004 | 2014-07-21 17:30:00 |
| received | OK, Received | +4407790000005 | 2014-07-21 17:45:00 |
+-----------+---------------------+----------------+---------------------+
答案 0 :(得分:0)
所以,鉴于这个中间结果,期望的结果集应该是什么样的?
SELECT *,'outgoing' source FROM messages_outgoing
UNION
SELECT *,'incoming' FROM messages_incoming;
+----+----------------+----------------+--------------------------+-----------+---------------------+----------+
| id | To | From | Body | SmsStatus | date | source |
+----+----------------+----------------+--------------------------+-----------+---------------------+----------+
| 1 | +4407790000000 | +4407790000001 | Outgoing Message | pending | 2014-07-21 17:10:00 | outgoing |
| 2 | +4407790000000 | +4407790000001 | Another outgoing message | pending | 2014-07-21 17:20:00 | outgoing |
| 3 | +4407790000000 | +4407790000001 | Final message sent | pending | 2014-07-21 17:30:00 | outgoing |
| 1 | +4407790000001 | +4407790000000 | Replying to first | received | 2014-07-21 17:15:00 | incoming |
| 2 | +4407790000001 | +4407790000000 | Replying to second | received | 2014-07-21 17:25:00 | incoming |
+----+----------------+----------------+--------------------------+-----------+---------------------+----------+