2012-01-19 3 views
0

selectに行がないときに "canned"行を返す必要があります。だから、SQL SQLite selectを使用してselectに行がないときに行を返す

select col1 from table where col1 = 'something' 

のようなものは、これは1行以上の罰金を返しますが、それは行が戻されない場合は、COL1として「何もない」のようなものを返す必要があります。私の狂気の理由は

答えて

0

...あなたは絶対にしなければならない場合はオラクルでは、私はこれを記述します。

select col1 from table 
where col1 = 'something' 
union all 
select 'Nothing' from dual 
where not exists (select 1 from table where col1 = 'something') 

私はsqliteのデュアルエミュレートする方法がわかりません。

アップデート:私は見ての通り は、fromがommitedすることができ、その答えは指定できます

select col1 from table 
where col1 = 'something' 
union all 
select 'Nothing' 
where not exists (select 1 from table where col1 = 'something') 
+0

これを使って、他の人がうまくいくかもしれない、素晴らしい作品です – ort11

0

は私に.....私は

select 
case when col1 is null 
then 
    'nothing' 
else 
    col1 
end as col1 
from table where col1 = 'something' 

を試してみました。しかし、処理する行がないので、これは「何も」を返しませんでしょう

....がありますこれは、データベース側で人為的な価値を強制しようとするのではなく、アプリケーションコードでよりうまく処理されます。結果セット内のゼロ行をテストし、その時点で置換を行うだけです。私が思い付くことができ

+0

私の狂気..... – ort11

0

だけの事は

RIGHTを追加
if exists(select col1 from table where col1 = 'something') 
select col1 from table where col1 = 'something' 
else 'nothing' 
1

であるあなたが必要なものを取得する必要があり、JOIN:

select COALESCE(col1, fake.filler) as col1 
from table 
    right join (select 'nothing' as [filler]) fake 
    on col1 = 'something' 

または左としては、(あまりエレガント)のJOIN:

select COALESCE(col1, fake.filler) as col1 
from (select 'nothing' as [filler]) fake 
    left join table 
    on col1 = 'something' 

確かに、@ Joe Stefanelliは、この動作がデータ取得レイヤーに属していないことは間違いありません。しかし

+0

+1素敵な理由は、私のような労働組合なし、あります。 –

+0

このクエリはSQL Serverで動作しますが、なぜそれに欠陥があるのか​​わかりません。 – PinnyM

+0

@PinnyM:コードを誤解しています。私のdownvoteを削除しました。 –

関連する問題