繁体   English   中英

SQL Select 其中所有相关条目都满足条件

[英]SQL Select where all related entries satisfy condition

表 A:

id | name | type 

表 B:

id | a_id | structure

其中 A 有许多 B。

我想查询所有 As 表 B 中没有任何相关条目的结构 = 'successful'

我正在尝试做

select a.name
from a
inner join b on a.id = b.a_id where a.type = 'note' and a.id = ALL (Select a_id from B where structure <> 'successful')

但我得到 0 个结果。 (Heroku 数据夹)

样本数据

id | name | type
01 | woof | note
02 | meow | note
03 | who  | free

id | a_id | structure
01 | 01 | open
02 | 01 | draft
03 | 02 | draft
04 | 02 | successful
05 | 02 | open
06 | 03 | open

运行此查询应返回

woof

因为我想要来自 A 的所有实体都具有关联类型的注释,但是表 B 中的所有相关条目都没有结构 = 'successful'

一个简单的not exists会满足您的标准吗?

select a.name
from TableA a
where a.type='note'
and not exists (select * from TableB b where b.a_id=a.id and b.structure='successful')

结果: “汪汪”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM