简体   繁体   English

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

[英]SQL Select where all related entries satisfy condition

Table A:表 A:

id | name | type 

Table B:表 B:

id | a_id | structure

where A has many B's.其中 A 有许多 B。

I would like to query for all As where none of it's related entries in Table B has a structure = 'successful'我想查询所有 As 表 B 中没有任何相关条目的结构 = 'successful'

I am attempting to do我正在尝试做

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')

but I am getting 0 results.但我得到 0 个结果。 (Heroku Dataclips) (Heroku 数据夹)

Sample Data样本数据

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

Running this query should return运行此查询应返回

woof

since I want all entities from A that has an associated type of note, but that none of it's related entries in table B has a structure = 'successful'因为我想要来自 A 的所有实体都具有关联类型的注释,但是表 B 中的所有相关条目都没有结构 = 'successful'

A simple not exists would satisfy your criteria?一个简单的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')

Result: 'woof'结果: “汪汪”

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

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