sql - SQL: return true/false if a related record is presented
问题描述
I have two tables:
ASSIGNMENTS (ID)
ASSIGNMENT_REVIEWS (ID, ASSIGNMENT_ID)
As a result of selecting I'd like to retrieve a flag if a review is already presented for the assignment. How to do it in the best way?
解决方案
You are looking for the exists statement:
select
id,
case when exists (
select 1 from assignment_reviews where assignment_reviews.assignment_id = assignments.id
) then 1 else 0 end as hasReview
from
assignments
推荐阅读
- c# - EF Core Sql 执行顺序需要改变
- css - 如何在 React 中使用带有样式组件的内联样式?
- algorithm - 如何找到图中顶点子集的“中心”?
- python - 从字符串创建十六进制列表
- sql - 将客户端-服务器应用程序转换为 Web 连接:REST 还是客户端查询?
- docker - 无法从 Docker 代理/镜像/缓存注册表中删除图像
- python - 如何从python中的字符串中分离出某些数字?
- python - 我应该如何处理这个异常 Python Sympy?
- windows - 为什么通过 FFI 接口从 Rust 调用 Ada DLL 时会卡在 adainit?
- java - Webflux 没有在我的 Atlas MongoDB(集群)中插入信息