MySQL查询两个表和最大时间戳

时间:2014-09-12 01:30:14

标签: mysql select timestamp

我有两张看起来像这样的表:

TABLE_conversations:

+-----------------+----------+----------------+------------+---------------------+--------+
| CONVERSATION_ID | QUEUE_ID | CONTACT_NUMBER | CONTACT_ID | DATE_CREATED        | STATUS |
+-----------------+----------+----------------+------------+---------------------+--------+
|               1 |        1 | 15551112222    |    9000001 | 2014-09-12 00:28:24 | ACTIVE |
|               2 |        1 | 15553334444    |    9000002 | 2014-09-12 00:32:08 | ACTIVE |
+-----------------+----------+----------------+------------+---------------------+--------+

TABLE_messages:

+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+
| MESSAGE_ID | CONVERSATION_ID | FROM_NUMBER | TO_NUMBER   | DIRECTION | SENDER  | TIMESTAMP          | VIEWED | MESSAGE                                                                                                         | STATUS |
+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+
|          1 |               1 | 15551112222 | 17021112222 | IN        | 9000001 | 2014-09-12 00:30:11 |      1 | Hello!  Is this working?                                                                     | ACTIVE |
|          2 |               1 | 17021112222 | 15551112222 | OUT       | 8000001 | 2014-09-12 00:31:05 |      1 | Good evening!  Of course!  | ACTIVE |
|          3 |               1 | 15551112222 | 17021112222 | IN        | 9000001 | 2014-09-12 00:31:27 |      1 | Perfect.  Thank you!                                                                                            | ACTIVE |
|          4 |               1 | 17021112222 | 15553334444 | OUT       | 8000002 | 2014-09-12 00:32:52 |      1 | Ticket 11251 is ready for pickup.                                                                         | ACTIVE |
+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+

我正在尝试运行查询以选择CONVERSATION_ID,CONTACT_NUMBER,CONTACT_ID和最新的TIMESTAMP并按电话号码分组:

SELECT TABLE_conversations.CONVERSATION_ID, TABLE_conversations.CONTACT_NUMBER,
       TABLE_conversations.CONTACT_ID, MAX(TABLE_messages.TIMESTAMP) 
FROM TABLE_conversations, TABLE_messages 
WHERE TABLE_conversations.STATUS='ACTIVE' 
AND TABLE_messages.STATUS='ACTIVE' 
GROUP BY CONTACT_NUMBER 
ORDER BY TABLE_messages.TIMESTAMP;

我得到的输出如下:

+-----------------+----------------+------------+-------------------------------+
| CONVERSATION_ID | CONTACT_NUMBER | CONTACT_ID | MAX(TABLE_messages.TIMESTAMP) |
+-----------------+----------------+------------+-------------------------------+
|               1 | 15551112222    |    9000001 | 2014-09-12 00:32:52           |
|               2 | 15553334444    |    9000002 | 2014-09-12 00:32:52           |
+-----------------+----------------+------------+-------------------------------+

我为两者获得了相同的TIMESTAMP。我想要的结果是2014-09-12 00:31:27 15551112222和2014-09-12 00:32:52 15553334444。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

您错过了表格之间的加入条件,因此您获得了完整的跨产品。因此,每个对话都与每条消息相结合,而不仅仅是来自该对话的消息。

SELECT TABLE_conversations.CONVERSATION_ID, TABLE_conversations.CONTACT_NUMBER,
       TABLE_conversations.CONTACT_ID, MAX(TABLE_messages.TIMESTAMP) 
FROM TABLE_conversations
JOIN TABLE_messages ON TABLE_conversations.conversation_id = TABLE_messages.conversation_id
WHERE TABLE_conversations.STATUS='ACTIVE' 
AND TABLE_messages.STATUS='ACTIVE' 
GROUP BY CONTACT_NUMBER 
ORDER BY TABLE_messages.TIMESTAMP;

答案 1 :(得分:0)

您的sql交叉连接两个表中的所有行,因此任何组的最大时间戳都是相同的。

SELECT TABLE_conversations.CONVERSATION_ID, 
       TABLE_conversations.CONTACT_NUMBER, 
       TABLE_conversations.CONTACT_ID, 
       MAX(TABLE_messages.TIMESTAMP) 
FROM TABLE_conversations
JOIN TABLE_messages 
ON TABLE_conversations.CONVERSATION_ID = TABLE_messages.CONVERSATION_ID 
WHERE TABLE_conversations.STATUS='ACTIVE' 
      AND TABLE_messages.STATUS='ACTIVE' 
GROUP BY CONTACT_NUMBER 

建议你删除group by和aggregation函数,看看完全交叉连接和内连接之间有什么不同。如:

SELECT TABLE_conversations.CONVERSATION_ID, 
       TABLE_conversations.CONTACT_NUMBER, 
       TABLE_conversations.CONTACT_ID, 
       TABLE_messages.TIMESTAMP
FROM TABLE_conversations
JOIN TABLE_messages 
ON TABLE_conversations.CONVERSATION_ID = TABLE_messages.CONVERSATION_ID 
--without on clause above, comes to the full cross join
WHERE TABLE_conversations.STATUS='ACTIVE' 
      AND TABLE_messages.STATUS='ACTIVE' 
ORDER BY TABLE_messages.TIMESTAMP;