gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:43:22 24 4
gpt4 key购买 nike

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

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 |
+-----------------+----------+----------------+------------+---------------------+--------+

表消息:

+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+
| 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 |
+-----------------+----------------+------------+-------------------------------+

我得到的两者的时间戳相同。我想要的结果是 2014-09-12 00:31:27 为 15551112222 和 2014-09-12 00:32:52 为 15553334444。

非常感谢任何帮助!

最佳答案

您缺少表之间的连接条件,因此您将获得完整的叉积。因此,每个对话都与每条消息相结合,而不仅仅是该对话中的消息。

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;

关于MySQL查询两个表和最大时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25799401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com