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 回答