0

特定の条件が真の場合はテーブルからいくつかの行を選択し、別の条件が真の場合は他の行を選択し、そうでない場合は(最終的に)他の行を選択します。主な問題は、次のようにコマンドラインからパラメーターを挿入したいことです:

if exists(select a.* from a
left join b on a.id=b.id
where b.id=:MY_PARAMETER)
else if exists
(select c.* from c where c.id=:Another_Parameter)
else
(select * from b)

私は何か間違ったことをしていることを理解していますが、何がわからないのですか。CASE-Then を使用してみましたが、ソリューションに適応する方法が見つかりませんでした。何か案が?ありがとう

PS: このようなことに関する他の投稿をいくつか読みましたが、説明したように、これで問題が発生しています。

<===編集=====>

私が何かを明確にしていることを願っています:

select
case when b.id=6
then (select * from a)
else (select a.* from a join b
on b.aid=a.aid)
end
from a join b
on b.aid=a.aid
join c
on b.id=c.bid
where b.id=:num

この場合の問題は、CASE ステートメントで複数の値を返すことができないことです。

4

3 に答える 3

1

たとえば、最初の例の場合、ユニオンは問題なく動作するはずです(これは、テーブル a、b、および c の列の順序と型が類似している場合にのみ機能します)。

select a.* from a
left join b on a.id=b.id
where b.id=:MY_PARAMETER
UNION
select c.* from c where c.id=:Another_Parameter
and not exists(select a.* from a
left join b on a.id=b.id
where b.id=:MY_PARAMETER)
UNION
select b.* from b
where not exists
(select c.* from c where c.id=:Another_Parameter
and not exists(select a.* from a
left join b on a.id=b.id
where b.id=:MY_PARAMETER))
and not exists (select a.* from a
left join b on a.id=b.id
where b.id=:MY_PARAMETER)

より効果的なクエリを作成するには、より具体的な例が必要です。


SELECT a.* FROM a
INNER JOIN b ON a.id = b.id
WHERE b.id = :MY_PARAMETER
UNION
SELECT a.* FROM a
INNER JOIN b ON a.id = b.id
INNER JOIN c ON b.id = c.bid
WHERE NOT EXISTS (SELECT * FROM b WHERE
                 b.id = :MY_PARAMETER)
AND c.id = :Another_Parameter
于 2013-08-19T08:59:00.050 に答える
0

DECODEを試しましたか?

Select DECODE(EXISTS(*Case1*),*First case column*,
    DECODE(EXISTS(*Case2*),*Second case column*,*Default case column*)) FROM ...
于 2013-08-19T12:10:51.910 に答える