【问题标题】:MySQL Query Two Tables and Max TimestampMySQL查询两个表和最大时间戳
【发布时间】:2014-09-12 01:30:14
【问题描述】:

我有两张类似这样的表格:

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。

非常感谢任何帮助!

【问题讨论】:

    标签: mysql select timestamp


    【解决方案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 | 15551112222 | 9000001 | 2014-09-12 00:32:52`
    • 忽略我的评论 - 您的查询完美运行!我对所有事情都使用了对话 ID 1,这就是为什么我只得到一条记录。感谢您的帮助!
    【解决方案2】:

    您的 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功能,看看full cross join和inner join有什么不同。如:

    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;
    

    【讨论】:

    • 感谢您的帮助。这似乎不起作用。我收到了四条记录(当我应该得到两条时),还有一些电话号码错误:1 | 15551112222 | 9000001 | 2014-09-12 00:30:11 | | 1 | 15551112222 | 9000001 | 2014-09-12 00:31:05 | | 1 | 15551112222 | 9000001 | 2014-09-12 00:31:27 | | 1 | 15551112222 | 9000001 | 2014-09-12 00:32:52
    • 我的错。去掉 order by 子句。
    猜你喜欢
    • 1970-01-01
    • 2014-01-28
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多