在postgres中按对选择不同的最后一条消息 [英] Select distinct last messages by pairs in postgres

查看:80
本文介绍了在postgres中按对选择不同的最后一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有消息的表:



 从消息
*
p>

这就是我得到的:





我确实得到了两个用户之间的唯一对话行,但不是最后一个。



如何只选择两个用户之间的最后一个唯一对话?



已编辑:预期输出:



解决方案

这是我的操作方式:

  SELECT DISTINCT ON(user_from,user_to)* 
FRO M条消息
ORDER BY user_from,user_to,date + time DESC


I have a table with messages:

I need to only one LAST by date and time conversation between two users and here is what i do:

select  distinct on (user_from, user_to)
*
from messages

Here is what I get:

I do get one unique conversations lines between two users, but its not the last one.

How should I select only the last one unique conversation between two users?

EDITED: expected output:

解决方案

Here is how I did it:

SELECT DISTINCT ON (user_from, user_to)   *
FROM messages
ORDER BY user_from, user_to, date+time DESC

这篇关于在postgres中按对选择不同的最后一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆