ESSAY / NOTE

mysql中case when语句的使用示例

下面为您举例说明了三种mysql中case when语句的使用方法,供您参考学习,如果您对mysql中case when语句使用方面感兴趣的话,不妨一看。 1。
    select name,  
     case   
            when birthday<'1981' then 'old'  
            when birthday>'1988' then 'yong'  
            else 'ok' END YORN  
    from lee; 
2。
    select NAME,  
     case name  
         when 'sam' then 'yong'  
            when 'lee' then 'handsome'  
            else 'good' end  
    from lee; 
当然了case when语句还可以复合 3。
    select name,birthday,  
     case   
         when birthday>'1983' then 'yong'  
            when name='lee' then 'handsome'  
            else 'just so so ' end  
    from lee;  
比如你有这样一张表 +----+-------+ | id | catid | +----+-------+ | 1 | 2 | | 2 | 1 | | 3 | 3 | +----+-------+ 现在你想要的结果为 +----+-------+ | id | catid | +----+-------+ | 1 | 1 | | 2 | 1 | | 3 | 3 | +----+-------+ SELECT id,CASE catid WHEN 2 THEN 1 ELSE catid END AS catid FROM 表a