首页 > 解决方案 > 确定是否在一个线程 MySQL 中读取所有消息

问题描述

假设您有一个如下所示的数据库模式:

Message: (id: int PK, read: boolean, thread_id: int FK, ...)

Thread: (thread_id: int PK, ...)

在消息中,读取值表示双方是否已查看消息。

您将如何构造一个返回每个 thread_id 的查询,以及另一个表示是否已读取属于该线程的每条消息的值。例如:消息:(1, 1, 1), (2, 1, 1), (3, 0, 1), (4, 1, 2), (5, 1, 2), (6, 1, 2 )

查询它会返回 (read, thread_ID) => (0, 1), (1, 2) 线程 1 的读取值为 0,因为一条消息未读。线程 2 的读取值为 1,因为已读取每条消息。

您将如何进行此查询?

标签: mysqlsqldatabase

解决方案


select thread_id, min(`read`) as `read` from message group by thread_id

推荐阅读