[SQL case when的两种用法]

第一种 格式 : 简单Case函数 :

格式说明    

    case 列名

    when   条件值1   then  选择项1

    when   条件值2    then  选项2.......

    else     默认值      end

eg:

    select 
    case   job_level
    when     '1'     then    '1111'
    when   '2'     then    '1111'
    when   '3'     then    '1111'
    else       'eee' end
    from     dbo.employee

第二种  格式 :Case搜索函数

格式说明    

    case  

    when  列名= 条件值1   then  选择项1

    when  列名=条件值2    then  选项2.......

    else    默认值 end

eg:

    update  employee
    set         e_wage =
    case
    when   job_level = '1'    then e_wage*1.97
    when   job_level = '2'   then e_wage*1.07
    when   job_level = '3'   then e_wage*1.06
    else     e_wage*1.05
    end

 

 Case When Then多条件判断SQL 语句

select      ID,Username,namer=(case when(score<='50')    then '实习'
 
                                    when(score>'50'  and  score<='500' )   then '赤脚医生'    
 
                                    when(score>'500'  and score<='1000' )   then '村卫生员' 
 
                                    when(score>'1000'  and score<='1500' )   then '乡卫生员' 
 
                                    when(score>'1500'  and score<='2000' )   then '镇卫生员'
 
                                    when(score>'2000'  and score<='3000' )   then '医师'
 
                                    when(score>'3000'  and score<='5000' )   then '主治医师'
 
                                    when(score>'5000'  and score<='10000' )   then '副主任医师'
 
                                    when(score>'10000'  and score<='20000' )   then '主任医师'
 
                                    when(score>'20000'  and score<='50000' )   then '健康大使'
 
                                    else   '健康大使'  end ), (SELECT count(id)
 
         FROM  jk01_YiWen_Question  
 
         WHERE  UserID =  dbo.jk01_Member.ID)  as  questionnum  
 
   from  jk01_Member

 

 

源文:https://www.cnblogs.com/shaopang/p/6903985.html

https://www.cnblogs.com/kevin2013/archive/2010/07/02/1769682.html

posted @ 2019-07-15 20:41  BloggerSb  阅读(573)  评论(0)    收藏  举报