mysql - 确定是否在一个线程 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,因为已读取每条消息。
您将如何进行此查询?
解决方案
select thread_id, min(`read`) as `read` from message group by thread_id
推荐阅读
- spring-webflux - WebFlux 不返回给定状态
- javascript - Javascript:日期选择器未出现在我的日历上
- angular - 如何为我的 ngrx 商店处理过滤器功能?
- flutter - 无法将用户从 Auth0 同步到 Hasura
- java - 检测动画何时结束(Android Studio)
- angular - 使用 strictNullChecks 从可能为 null 的属性中键入别名
- html - Bootstrap 4 修复导航栏的问题
- php - 如何为 PATHINFO() 添加 readline 函数(扩展)
- node.js - 如何调用另一个类的函数或导出的对象?
- android - Firebase 实时数据库 - ChildEventListener - 基础