sql - 如果另一个表中存在一个行 ID,则跳过所有行
问题描述
我有三个具有这种关系的表:
T1.journo = T2.journo
T2.recid = T3.spid
T1
ticketno journo
1 A1
2 A2
T2
journo recid
A1 1
A1 2
A1 3
A2 4
A2 5
A2 6
T3
spid
2
我只想要 T3.spid 中不存在 T2.recid 的 T1 条目。
下面的查询将省略 T2 的第二行。但是我希望省略 journo=A1 的所有 T2 行,因为 T3 中存在 A1 的一个记录。
select T1.* from T1 join T2 on T1.journo = T2.journo
where T2.recid not in (select spid from T3)
期望的输出:
ticketno journo
2 A2
有小费吗?
解决方案
对我来说,这听起来像not exists
:
select t1.*
from t1
where not exists (select 1
from t2 join
t3
on t2.recid = t3.spid
where t2.journo = t1.journo
);
推荐阅读
- javascript - 如何为角度 10 中的重复字段集构建或构建表单?
- ios - 如何正确注册GestureConflictWithZoom()?
- javascript - 在 Django 项目中,通过 JavaScript 进行样式设置不起作用
- spring-boot - 如何在 Java Spring boot 中将 MultipartFile 照片作为 Base64 字符串插入 DB 并将其从 db 显示在主屏幕上
- javascript - react js中基于表单的行动计划
- unity3d - 我的角色在滑动,不播放动画(Unity3d、Animator、Blend Tree、Animation)
- visual-studio-code - 使用 Chocolatey 安装主题套件(主题版本主题:无法识别术语“主题”)
- python - 直接使用python将图像从链接保存到ftp服务器是否可行
- react-native - 我可以将道具传递给
React Native 中的组件? - javascript - 如何将javascript异步函数的返回值传递给python变量