简体   繁体   English

如何 select 全部从一个表中按最后创建的时间戳从另一个表中的值排序

[英]How to select all from one table order by the last created timestamp of values from another table

I have 2 tables called conversation and message .我有 2 个表,称为conversationmessage

I would like to select all conversations sorted by order of the last updated message related to the conversation.我想 select 所有对话按与对话相关的最后更新消息的顺序排序。 The conversation table doesn't have the updated_at and to be able to get all conversations ordered by the last updated_at, that should be deducted from the last created message in the conversation from the table message.会话表没有 updated_at 并且能够获取按最后一个 updated_at 排序的所有会话,这应该从表消息中的会话中最后创建的消息中扣除。

The result should be based on what is the last message of the conversation.结果应基于对话的最后一条消息。

The SQL I tried is not giving me the right result and I believe I should use a join but as I'm new to SQL cannot really get it how我试过的 SQL 没有给我正确的结果,我相信我应该使用连接,但因为我是 SQL 的新手,所以无法真正了解它

select
    *
from
    conversation
inner join message on
    conversation.id = message.conversation_id
order by
    message.updated_at desc;    

Tables looks as follow表格如下所示

The conversation谈话

id ID topic话题 origin_type origin_type origin_id origin_id created_at created_at
1 1 t1 t1 doc1文档1 xx1 xx1 2021-04-07 13:23:40 2021-04-07 13:23:40
2 2 t2 t2 doc2文档2 xx2 xx2 2021-04-07 14:23:40 2021-04-07 14:23:40
3 3 t3 t3 doc3文档3 xx3 xx3 2021-04-07 15:23:40 2021-04-07 15:23:40

Message table消息表

id ID conversation_id对话ID message信息 created_at created_at updated_at更新时间
1 1 1 1 xxx xxx 2021-04-07 13:23:40 2021-04-07 13:23:40 2021-04-07 13:23:40 2021-04-07 13:23:40
2 2 2 2 xxx xxx 2021-04-07 14:23:40 2021-04-07 14:23:40 2021-04-07 14:23:40 2021-04-07 14:23:40
3 3 3 3 xxx xxx 2021-04-07 15:23:40 2021-04-07 15:23:40 2021-04-07 15:23:40 2021-04-07 15:23:40

Seeing the tables I should get a result of the select as follow I believe as the conversation.id = 3 has the last created and most recent message.看到表格我应该得到 select 的结果,如下所示,我相信因为 conversation.id = 3 有最后创建和最新的消息。

id ID conversation_id对话ID
1 1 3 3
2 2 2 2
3 3 1 1

In SQL/PSQL I don't know how to do this kind of selection在 SQL/PSQL 中我不知道如何进行这种选择

Your data is not particularly illustrative, because the conversations only have a single message.您的数据不是特别说明性的,因为对话只有一条消息。 However, I think you want to keep all rows for a conversation together.但是,我认为您希望将所有行保持在一起进行对话。 That would use a window function to get the maximum updated time for each conversation:这将使用 window function 来获得每个对话的最大更新时间:

order by max(message.updated_at) over (partition by conversation.id) desc,
         conversation.id,
         message.updated_at desc

EDIT:编辑:

If you just want the conversations with no message information, you can use a subquery:如果您只想要没有消息信息的对话,您可以使用子查询:

select c.*
from conversations c
order by (select max(m.updated_at) from messages m where m.conversation_id = c.id) desc;

You can just use a scalar subquery:您可以只使用标量子查询:

select *,
    (
        select max(m.updated_at) from message m
        where m.conversation_id = c.conversation_id
    ) as last_updated_at
from conversation c
order by last_updated_at desc;  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL SELECT值从一个表中的列按顺序(一个接一个)到另一个表中的列 - SQL SELECT Values from column in one table to a column in another table in order (one by one) 从一个表中进行选择,在另一个表上进行联接,在第三个表中进行排序? - SELECT from one table, JOIN on another table, ORDER BY a third table? 从一个表中选择并按另一表的列排序 - Select from one table and order by column of another table 从一个表中选择全部,然后在结果中填充另一个表中的值 - Select All from one table and fill values from another table on the result 从一个表中选择所有记录,并从另一表中选择引用值? - Select all records from one table and referenced values from another table? 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? Select 一个表的所有列和另一个表的一些列 - Select all columns from one table and some from another table 如何从一个表中选择最后一行,然后更新另一个表的最后一行? - How to select the last row from one table then update the last row of another one? 如何从一个表中选择字段并按另一表中的字段排序 - How to select fields from one table and order them by fields from another table 从一个表中选择所有值并参考另一个表,但是该值丢失 - SELECT all values from one table with reference to another table but the value there is missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM