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

Sql 如果多个表存在记录,如何返回布尔值,sql,oracle,Sql,Oracle,如果记录存在,我想返回布尔值。 下面的查询适用于单个表 SELECT CASE WHEN MAX(componentid) IS NULL THEN 'NO' ELSE 'YES' END table3 FROM table3 WHERE componentid = 'GetAccountBalance'; 我使用JOIN对3个表进行了相同的尝试,但未能达到预期的结果。如果其中任何一个表(假设表3)没有记录,而另两个表有记录,则下面的查询将所有值返回为“否” select CASE WHEN

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

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

我使用JOIN对3个表进行了相同的尝试,但未能达到预期的结果。如果其中任何一个表(假设表3)没有记录,而另两个表有记录,则下面的查询将所有值返回为“否”

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
也可以在中搜索多个值吗?像

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

请查找以下查询:

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';
是的,您可以在('GetAccountBalance','GetCreditBalance')中使用:
和a.componentid


这将对您有所帮助。

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

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');
如果仅在('GetAccountBalance','GetCreditBalance')中使用
a.componentid('GetAccountBalance','GetCreditBalance')
,如果值在
表1中根本不存在,则结果将不会有任何行

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

如果
componentid
在每个表中都不是唯一的,那么每个表可能会有多行

SQLFIDLE示例:

(该示例使用数字表示componentid,因为我懒得键入字符串)

您应该将其表述为
存在

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

如果联接表,则无法对sql中的每个表执行最大值。这些表被合并在一起,您将得到一个合并的结果。简单地说:您的MAX操作符在这里是错误的。您应该将其拆分为单独的SQL。您以前使用过JOIN吗?
componentid
在每个表中都是唯一的吗?是否该值可能不存在于
表1
?@a_horse_,您的两个问题的名称都为“yes”。Thanks@Surya只需使用一个并集即可获得多个组件。