如果多个表存在记录,如何返回布尔值

时间:2016-01-10 09:09:03

标签: sql oracle

如果记录存在,我想返回布尔值。 以下查询适用于单个表。

SELECT CASE WHEN MAX(componentid) IS NULL THEN 'NO' ELSE 'YES' END
table3 FROM table3 WHERE componentid = 'GetAccountBalance';

我使用JOIN对3个表进行了同样的尝试但是我无法达到预期的结果。如果任何表(假设table3)没有记录,则下面的查询将所有值都返回为“No”另外两张表有。

select CASE WHEN MAX(a.componentid)IS NULL THEN 'NO' ELSE 'YES' END table1,
CASE WHEN MAX(b.componentid)IS NULL THEN 'NO' ELSE 'YES' END table2,
CASE WHEN MAX(c.componentid)IS NULL THEN 'NO' ELSE 'YES' END table3
from table1 a
join table2 b on a.componentid=b.componentid 
join table3 c on a.componentid=c.componentid 
and a.componentid ='GetAccountBalance';

输出

table1 table2 table3
NO     NO     NO

预期

table1 table2 table3
YES    YES    NO

还可以使用in搜索多个值吗?像

a.componentid in ('GetAccountBalance','GetCreditBalance')

3 个答案:

答案 0 :(得分:1)

您希望所有三个表都有外连接。您还需要包含一个条件,用于检查所有三个表中是否存在值:

select coalesce(a.componentid, b.componentid, c.componentid) as componentid,
       case when a.componentid is null then 'no' else 'yes' end as in_table1,
       case when b.componentid is null then 'no' else 'yes' end as in_table2,
       case when c.componentid is null then 'no' else 'yes' end as in_table3
from table1 a
  full outer join table2 b on a.componentid = b.componentid
  full outer join table3 c on b.componentid = c.componentid
where a.componentid in ('GetAccountBalance','GetCreditBalance')
   or b.componentid in ('GetAccountBalance','GetCreditBalance')
   or c.componentid in ('GetAccountBalance','GetCreditBalance');

如果您只使用where a.componentid in ('GetAccountBalance','GetCreditBalance'),那么如果table1中的值根本不存在,则结果将不会包含任何行。

这不会返回任何表中的值的行!

如果每个表中componentid 唯一,则每个表可能会有多行。

SQLFiddle示例:http://sqlfiddle.com/#!4/c70b3e/1
(该示例使用数字进行组件,因为我懒得键入字符串)

答案 1 :(得分:1)

您应将其标记为exists

select (case when exists (select 1 from table1 where componentid = 'GetAccountBalance')
             then 'YES' else 'NO'
        end) as flagTable1,
       (case when exists (select 1 from table2 where componentid = 'GetAccountBalance')
             then 'YES' else 'NO'
        end) as flagTable2,
       (case when exists (select 1 from table3 where componentid = 'GetAccountBalance')
             then 'YES' else 'NO'
        end) as flagTable3
from dual;

进行连接的开销根本就没有必要。以上内容也应该在表格上最佳地使用索引。

编辑:

对于多个组件,您可以使用相关子查询:

select (case when exists (select 1 from table1 t1 where t1.componentid = c.componentid)
             then 'YES' else 'NO'
        end) as flagTable1,
       (case when exists (select 1 from table2 t2 where t2.componentid = c.componentid)
             then 'YES' else 'NO'
        end) as flagTable2,
       (case when exists (select 1 from table3 t3.where t3.componentid = c.componentid)
             then 'YES' else 'NO'
        end) as flagTable3
from (select 'GetAccountBalance' as componentid from dual union all
      select 'GetCreditBalance' from dual
     ) c

答案 2 :(得分:0)

请查看以下查询:

select CASE WHEN MAX(a.componentid)IS NULL THEN 'NO' ELSE 'YES' END table1,
CASE WHEN MAX(b.componentid)IS NULL THEN 'NO' ELSE 'YES' END table2,
CASE WHEN MAX(c.componentid)IS NULL THEN 'NO' ELSE 'YES' END table3
from table1 a
join table2 b on a.componentid=b.componentid 
left outer join table3 c on a.componentid=c.componentid 
and a.componentid ='GetAccountBalance';

是的,您可以使用:and a.componentid in ('GetAccountBalance','GetCreditBalance')

它会帮助你。