簡體   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