1

ネストされたクエリに「変数名」を割り当てて、次のクエリを書き直したいと思います。

select lastname 
  from table2 
 where firstname = (select name from table1 where val="whatever")

クエリを次のようにします。

( select name 
    from table1 
   where val = "whatever") as retrieved_name;

select lastname 
  from table2 
 where firstname = retrieved_name

純粋なSQLで可能ですか?

4

3 に答える 3

3

純粋なSQLで可能ですか?

いいえ。

「純粋なSQL」の変数はありません。Oralce-PL\SQL などで変数を持つことができます。

于 2012-05-06T12:18:31.000 に答える
1

joinサブセレクトの代わりに aを使用する意思があり、 MySQL を使用していない場合は、CTE (Common Table Expression) を使用できます。

with retrieved_name as ( 
  select name 
    from table1 
   where val = "whatever" 
         )
select lastname
  from table2 t2
  join retrieved_name t1
    on t2.firstname = t1.name

a_horse_with_no_name が指摘したように、これは次と同じです。

select lastname
  from table2 t2
  join ( select name 
           from table1 
           where val = "whatever" ) t1
    on t2.firstname = t1.name

クエリがばかげている場合を除き、実際には CTE よりも 2 番目の例のインライン ビューを好みますが、それは単なる個人的な好みです。

于 2012-05-06T12:29:30.650 に答える
0

代わりにこのようなことができます

select name as retrieved_name from table1 where val="whatever"); select lastname from table2 where firstname=retrieved_name

于 2012-05-06T12:25:48.553 に答える