我有這個查詢從兩個不同的連接中選擇資料
select count (*) from table1
where condition1 = 'answer1'
and time >= sysdate - interval '5' minute
UNION ALL
select count (*) from table2
where condition2 = 'answer2'
AND NOT EXISTS (
select * from table1
where condition = 'answer'
and time >= sysdate - interval '5' minute
);
我的問題是即使 table1 中沒有資料,table2 select 也根本不起作用。
任何人都可以幫助我讓它作業,或者知道另一種撰寫查詢的方式。
這個想法是,如果選擇計數回傳 0,那么從不同的表中進行另一個選擇
試著用谷歌搜索我的方式,甚至潛伏在這里,但仍然沒有得到任何答案
uj5u.com熱心網友回復:
如果table2沒有連接到子句中table1的某個NOT EXISTS地方,它將永遠不會回傳資料,因為只要查詢回傳它存在的東西。
如果您沒有一種鏈接table2方式table1,您可以將其用作不存在的連接,您可以執行以下操作
select count (*) from table 1
where condition = answer
and time >= sysdate - interval '5' minute
UNION ALL
select count (*) from table2
where condition2 = answer2
AND (
select count(*) from table 1
where condition = answer
and time >= sysdate - interval '5' minute
) = 0;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/523922.html
標籤:sql甲骨文oracle-sqldeveloper联合所有不存在
上一篇:有沒有辦法在不向結果查詢中添加列的情況下計算資料,同時仍按它進行過濾?
下一篇:有條件地抑制編譯器“空檢查”警告
