3

我正在通过 Oracle DB 使用 iReport 创建报告。我必须根据这样的条件选择一些值:

AND EXISTS (SELECT 1 FROM TABLE_1 WHERE x = y)  
OR EXISTS (SELECT 1 FROM TABLE_2 WHERE z = y) 

只有当第一个为假时才可以执行第二个 EXISTS 吗?

4

3 回答 3

7

尝试在括号中使用您的条件:

AND (
        EXISTS (SELECT 1 FROM TABLE_1 WHERE x = y)  
     OR EXISTS (SELECT 1 FROM TABLE_2 WHERE z = y)
    )
于 2013-07-19T10:01:51.270 回答
2

使用案例

where 
 ...
 AND 1 = case
          when EXISTS (SELECT 1 FROM TABLE_1 WHERE x = y)  then 1
          when EXISTS (SELECT 1 FROM TABLE_2 WHERE z = y)  then 1
          else 0
         end
于 2013-07-19T10:09:08.020 回答
0
AND (case when (SELECT count(*) FROM TABLE_1 WHERE x = y) > 0 then true
when (SELECT count(*) FROM TABLE_2 WHERE z = y) > 0 then true 
else false end)
于 2013-07-19T10:08:04.783 回答