select case when flag=0 then id when flag=1 then name end as name,age
from tablename

解决方案 »

  1.   

    如果id是数字型select case when flag=0 then cast(id as varchar(100)) when flag=1 then name end as name,age
    from tablename
      

  2.   

    如果一个表里有四个列,如:ID,Name,Age,Flag, 
    能不能在查询的时候判断Flag的值如过是0就输出Id,Age的值,
    如果是1就返回Name,Age的值?
    -----------------------------------------------------
    select 
    case flag when 0 then ID else Name as col1,
    Age,
    Flag 
    from tablename
      

  3.   

    问题解决啦,谢谢两位!! :)
    i9988(冒牌j9988 V0.2) 老师您的这个as那提示错误啊?现在这样如果可以,是重命名两个列;要是只把其中一列重新命名怎么写啊?select case when flag=0 then id when flag=1 then name end as name,age
    from tablename
      

  4.   

    select replace(replace(Flag,0,[ID]),1,[name]) as col1 ,age
      

  5.   

    to:giftzheng
    这个方法很巧!
      

  6.   

    to:giftzheng() 又了解一个方法,呵呵,虽然效率可能... 但也是一种思路啊