[英]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 个表,称为
conversation
和message
。
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 ![]() |
topic![]() |
origin_type ![]() |
origin_id ![]() |
created_at ![]() |
---|---|---|---|---|
1 ![]() |
t1 ![]() |
doc1![]() |
xx1 ![]() |
2021-04-07 13:23:40 ![]() |
2 ![]() |
t2 ![]() |
doc2![]() |
xx2 ![]() |
2021-04-07 14:23:40 ![]() |
3 ![]() |
t3 ![]() |
doc3![]() |
xx3 ![]() |
2021-04-07 15:23:40 ![]() |
Message table消息表
id ![]() |
conversation_id![]() |
message![]() |
created_at ![]() |
updated_at![]() |
---|---|---|---|---|
1 ![]() |
1 ![]() |
xxx ![]() |
2021-04-07 13:23:40 ![]() |
2021-04-07 13:23:40 ![]() |
2 ![]() |
2 ![]() |
xxx ![]() |
2021-04-07 14:23:40 ![]() |
2021-04-07 14:23:40 ![]() |
3 ![]() |
3 ![]() |
xxx ![]() |
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 ![]() |
conversation_id![]() |
---|---|
1 ![]() |
3 ![]() |
2 ![]() |
2 ![]() |
3 ![]() |
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.