列名

2013-10-31 26 views
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 

我需要命名的列名,但由於選擇的情況下運行查詢時,該列的顯示名稱沒有出現...

我該如何命名此列以顯示?

回答

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