有同事問我上述問題,我把我的實現思路寫出來。
子查詢把查詢的結果和默認的結果全部顯示。
父查詢通過偽列rownum來篩選,如果查詢有結果,就有幾條就顯示幾條,而不去顯示子查詢中的默認值;如果查詢沒有結果,那就把默認值顯示出來
舉例:
select * from (
select table_name from user_tables where rownum < 3
union all
select 'default' from dual )
where rownum <= (select case (select count(1) from user_tables where rownum <3) when 0 then 1 else (select count(1) from user_tables where rownum <3) end from dual);
這個查詢,對user_tables的限制條件需要一樣。通過調整限制條件中的記錄數量,可以實現:如果查詢有結果集,則有幾個就顯示幾個,如果查詢沒有結果集,就顯示default
