如何根据某个字段的值来输出不同的内容?例如某字段为 flag 当flag = 1 的时候输出夏天
等于2时输出秋天
等于3时输出冬天
等于4时输出春天 ...这样的SQL怎么写啊 ??

解决方案 »

  1.   

    select case when flag = 1 then '复天'
                when flag =2 then '秋天' 
                when flag = 3 then '冬天' 
                when flag = 4 then '春天'  
      end as '四季'
    或decode(flag,1,'复天',2,秋天,3,'冬天',4,'春天')
      

  2.   

    select decode(flag,1,'夏天',2,'秋天',3,'冬天',4,'春天') from table 
      

  3.   

    这个应该和sql无关!呵呵!数据库里某表某字段存贮1,2,3,然后你查询出来,=1就显示夏天……呵呵!
      

  4.   

    楼上两位操作太快,用decode和case 都可以呵呵
      

  5.   

    decode, case when都可以搞
    SQL> select * from test;FLAG
    ----------
    1
    2
    3
    2SQL>  select decode(flag, '1', '春天', '2', '夏天','其它') from test;DECO
    ----
    春天
    夏天
    其它
    夏天
      

  6.   


    select decode(id,1,'复天',2,'秋天',3,'冬天',4,'春天')
    from aselect case when id = 1 then '复天'
            when id = 2 then '秋天' 
            when id = 3 then '冬天' 
            when id = 4 then '春天'  
           end  四季
           from a
           
           
    select case id
            when   1 then '复天'
            when   2 then '秋天' 
            when  3 then '冬天' 
            when  4 then '春天'  
           end  四季
           from a
    三种写法