0

次のクエリがあります

    select * from table_1 
    where Conditions in 
         (select case when check_condition = 'Y' then 'Condition_1' 
                 else 'N/A' end as Check_condition 
          from table_2 
          WHERE id = 1122)

ここで、table_1 には次のように column の値が含まConditionsれます。条件_1,条件_2

これは正常に機能し、結果を返します。

句内で複数の選択ステートメントを使用したいinので、以下のようにしました。

    select * from table_1
    where Conditions in (
         select ''''||
             (select case when check_condition = 'Y' then 'Condition_1' 
               else 'N/A' end as Check_condition 
               from table_2 
               WHERE id = 1122)||''''||','''||
                  (select case when check_condition = 'Y' then 'Condition_2'
                        else 'N/A' end as Check_condition 
                  from table_2 WHERE id = 1122)||''''
                 from dual
                )

内部クエリ( in 句内)で、期待どおりの正しい結果が得られます-

'Condition_1','Condition_2' 

親クエリにコピーして貼り付けると、正常に動作し、結果が表示されます。

select * from table_1 where Conditions in ('Condition_1','Condition_2')

私の問題は、2 番目のクエリを使用したときに結果が得られないことです。サブクエリは、外側のクエリの行と一致する必要がある結果を返すことを知っています。しかし、空の結果セットが表示されます。

私はオラクル11gを使用しています

誰でも私を助けてくれませんか.. よろしくお願いします。

4

2 に答える 2

4

質問は要件に関して少し不明確です。あなたが望むのは、table1次の場合にのみレコードを選択することです。

  • 行は「Condition_1」または「Condition_2」に一致します
  • check_condition= 'Y'
  • table2ID = 1122の行があります

あなたの質問からcheck_conditionは、が列なのか変数なのか、それがどのテーブルに属している列なのかが明確ではありません。したがって、この解決策は間違っている可能性がありますが、原理を示しています。

select * from table1 t1
where t1.conditions in ('Condition_1','Condition_2')
and t1.check_condition = 'Y'
and exists
        ( select null from table2 t2
          where t2.id = 1122 )

これで必要な解決策が得られない場合は、質問を修正して、実装する必要があるビジネス ロジックを記述し、関連するテーブルの説明も含めてください。

于 2013-07-26T08:26:18.720 に答える
1

in手動で行う場合のように、句に 2 つの値が渡されることはありません。

select * from table_1 where Conditions in ('Condition_1','Condition_2')

値を連結した単一の値を渡しています。

select * from table_1 where Conditions in ('''Condition_1'',''Condition_2''')

condition連結された値に一致するものがないため、結果は得られません。次のようなことができます。

select * from table_1 where Conditions in (
  select case when check_condition = 'Y' then 'Condition_1' else 'N/A' end
  from table_2 WHERE id = 1122
  union all
  select case when check_condition = 'Y' then 'Condition_2' else 'N/A' end
  from table_2 WHERE id = 1122
)

または、おそらく、あなたがやっていることに従えば(データモデルを理解しているかどうかわからないので疑わしいです!):

select * from table_1 where check_condition != 'Y' or Conditions in (
  select 'Condition_1' from table_2 WHERE id = 1122
  union all
  select 'Condition_2' from table_2 WHERE id = 1122
)

結合を使用すると、これをよりきれいに実行できるはずですが、何が起こっているのかをもう少し理解するには、構造とサンプル データを確認する必要があると思います。

于 2013-07-26T08:19:47.993 に答える