2016-01-10 36 views
3

如果記錄存在,我想返回布爾值。 以下查詢適用於單個表格。如果記錄存在多個表如何返回布爾值

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

我曾嘗試同爲使用JOIN 3個表,但我不能做到以下查詢預期result.The返回所有值「否」如果任何表(表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') 
+2

如果你JOIN表,你不能做一個MAX上的每一個表中的SQL。這些表格連接在一起,並且您有一個合併的結果。簡單地說:你的MAX-Operator在這裏是錯誤的。你應該把它分解成獨立的SQL。你有沒有使用過JOIN? – etalon11

+0

是每個表中唯一的「componentid」嗎? 'table1'中可能不存在這個值嗎? –

+0

@a_horse_with_no_name'yes'for your question.Thanks –

回答

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 
+0

@Surya只需使用聯合來獲得多個組件。 – shawnt00

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')

它會幫助你。

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
(本例使用號碼的ComponentID因爲我懶得輸入字符串)