7

这是一个例子 T(A) = RENTED(A,C) / BOATS(C)

select distinct R1.A from RENTED R1  
where not exists                     
  (select * from SAILBOAT S     
   where not exists                  
     (select * from RENTED R2        
      where R1.A = R2.A              
        and R2.C = S.C)              
   );

我的问题是,如果NOT EXISTS只是返回TRUEor FALSE,如何SELECT distinct R1.A知道要返回哪些值?

例如这个 jsfiddle

如果存在 number = 5,则此查询将在 numbers 列中返回 EVERYTHING

4

1 回答 1

2

As wildplasser and sqlvogel have mentioned, the subquery gets executed once for each row in the outer query.

The result of the subquery (TRUE / FALSE) determines whether the row in the outer query would be returned. Invariably, the parent key (identifier) columns of the outer query would be referenced within the subquery to check its existence in other tables. This reference makes the subquery a "correlated subquery".

Please see the updated fiddle.

于 2014-03-25T14:57:25.427 回答