1
    SELECT favorite_id,MO,name,image_id,case image_id 
                    when 0 then (select image_path as image_path from images where image_id in (select default_image from registration where reg_id=9))
                    else (select image_path as image_path from images where image_id=b.image_id and active=1)
                    end
    FROM buddies b where reg_id=9 

この選択で:

   select image_path as image_path

列名に名前を付ける必要がありますが、クエリの実行時に列の表示名が表示されない場合の選択のため...

この列に表示用の名前を付けるにはどうすればよいですか?

4

1 に答える 1

2

case ステートメントの後に列エイリアスを追加します。

 SELECT 
     favorite_id,
     MO,
     name,
     image_id,
     case image_id 
         when 0 then (select image_path from images where image_id in (select 
         default_image from registration where reg_id=9))
         else (select image_path from images where image_id=b.image_id
         and active=1)
     end as alias_name_here
  FROM buddies b where reg_id=9 

SQL フィドル: http://sqlfiddle.com/#!2/5562d4/1

于 2013-10-31T08:57:12.660 に答える