2017-11-23 2 views
3

は、我々は、テーブルのDVDを持っていると仮定します。1つのクエリで2つの要求を組み合わせることは可能ですが、最初のクエリが何も持たない場合にのみ2つ目の要求が実行されますか?

id title     type  
1 Star Wars    Movie 
2 Yellow Submarine  Music 
3 The Lord of The Rings Movie 
4 Black Butterfly   Music 

私たちは、映画「黒蝶」でDVDを取得したいが、それはリストに存在していない場合、我々は他のフィルムを取得したいです。 ファース要求:

Select * from DVDs where type='Movie' and title='Black Butterfly' 

要求は何も返さない場合は、第2の要求を実行します。

Select * from DVDs where type='Moview' 

現時点では、私は(Javaで)2つのクエリテンプレートと2つのデータベース(Oracle)へのリクエストを使用しています。私は1つのテンプレートと1つのリクエストを使用する機会を探しています。あなたが行うことができます

答えて

2

:また

with b as (
     Select * 
     from DVDs 
     where type = 'Movie' and title = 'Black Butterfly' 
    ) 
select b.* 
from b 
union all 
select d.* 
from dvd 
where type = 'Movie' and not exists (select 1 from b); 

を、あなたは、ウィンドウ関数を使用することができます。

Select . . . 
from (select d.*, 
      count(*) filter (where title = 'Black Butterfly') over() as cnt_bb 
     from DVDs d 
     where type = 'Movie' 
    ) d 
where cnt_bb = 0 or title = 'Black Butterfly'; 
2

試してみてください。

DO 
$do$ 
BEGIN 
IF EXISTS (Select * from DVDs where type='Movie' and title='Black Butterfly') THEN 
    Select * from DVDs where type='Movie' and title='Black Butterfly'; 
ELSE 
    Select * from DVDs where type='Moview'; 
END IF; 
END 
$do$ 
+0

ORA-00900:無効なSQL文を – Micah

関連する問題