MYSQL 查询 : get the latest date with grouped by data

标签 mysql sql

我有一张聊天 table 。


消息 I senderID I recipentID I date

我想通过对话获取最新消息组的查询。 (例如,如果 'senderID = 1 and the recipentID = 2''senderID = 2 and the recipentID = 1' 是同一个对话)

最佳答案

您可以在中使用。 MySQL 支持带有 in 的元组,所以这是一种方法:

select c.*
from chats c
where (least(senderID, recipentID), greatest(senderID, recipentID), date) in
       (select least(senderID, recipentID), greatest(senderID, recipentID), max(date)
        from chats c
        group by least(senderID, recipentID), greatest(senderID, recipentID)
       );

关于MYSQL 查询 : get the latest date with grouped by data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52478276/

相关文章:

MySQL - 当 SELECT 返回多列时,根据 SELECT 的结果更新一列

mysql - IDbConnection 与 .Net Core 中的 MySql - 无法使用 autofac 注入(inject)

c++ - 如何从 ADO/C++ 调用 MSSQL 系统函数?

c# - 如何将 SQL 'LIKE' 与 LINQ to Entities 一起使用?

php - 我应该使用哪种方法,为什么?

python - Django : Duplicate entry in uuid4

php - 在 App Engine for PHP 中优化 MySQL 连接的推荐方法是什么?

MySQL 报告 - 编辑器

sql - 将 SQL Server 中已弃用的外连接运算符 *= 转换为左连接

javascript - 对 MySQL 运行数据库查询后无法看到结果