case when用法

case when的基本使用:

Case when 的用法: 一旦满足了某一个WHEN, 则这一条数据就会退出CASE WHEN , 而不再考虑 其他CASE。

Case when 的用法

-- -搜索Case函数:

Case函数(Case搜索函数): 判断表达式的真假,如果为真,返回结果;如果为假,返回else值;如果未定义else值,则返回空值(使用条件确定返回值);

select name,id,(case when id=34 then salary*2

                                    when id=45 then salary*3

                                    else salary

                                    end) new_salary

from semp;

-- -简单Case函数

简单Case表达式的作用是: 使用表达式确定返回值:

select id,name,(case sex

       when '1' then '男'

       when '2' then '女'

        else '其他' end)

from student;

 这两种方法可以实现相同的功能. 简单Case函数的写法比较简单,但是和case搜索函数相比,功能方面会有些限制,比如判断式。

还有一个需要注意的问题,Case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略。

比如下面 SQL,将无法得到第二个结果:(差永远获取不到)。

sql如下

case when colum in ('a', 'b') then '优秀'

         when colum in ('a') then '差'

          else '其他' end 
 

case when的使用场景有三种:

  • 等值转换
  • 范围转换
  • 列转行操作

1.等值转换

设计数据库的时候总会把用户的性别用int存储('0’为女,'1’为男),但是怎么把它抓换成汉字显示呢?

 
  1. select name as '姓名',

  2. (case sex when 0 then '女' else '男' end) as '性别'

  3. from test.student;

查询结果:

2.范围转换

按照用户成绩显示优(90分以上)、良(80分-90分)、及格(60分-80分)、不及格(60分一下):

 sql语句:

 
  1. select name as '姓名',

  2. (case when score > 90 then '优' when score >= 80 then '良' when score >= 60 then '及格' else '不及格' end) as '等级'

  3. from test.stu_score;

运行结果:

3.列转行操作

表一

 
  1. +----------+---------+------+

  2. | class_id | grade | rate |

  3. +----------+---------+------+

  4. | abc123 | primary | 70% |

  5. | abc123 | middle | 65% |

  6. | abc123 | high | 72% |

  7. | hjkk86 | primary | 69% |

  8. | hjkk86 | middle | 63% |

  9. | hjkk86 | high | 74% |

  10. +----------+---------+------+

 表二

 
  1. +----------+---------+--------+------+

  2. | class_id | primary | middle | high |

  3. +----------+---------+--------+------+

  4. | abc123 | 70% | 65% | 72% |

  5. | hjkk86 | 69% | 63% | 74% |

  6. +----------+---------+--------+------+

 将表一转换成表二显示:
第一步 其中三条CASE WHEN 语句是同步执行的

 
  1. select class_id,

  2. (case when grade = 'primary' then rate else 0 end) as 'primary',

  3. (case when grade = 'middle' then rate else 0 end) as 'middle',

  4. (case when grade = 'high' then rate else 0 end) as 'high'

  5. from mst_class;

  6. +----------+---------+--------+------+

  7. | class_id | primary | middle | high |

  8. +----------+---------+--------+------+

  9. | abc123 | 70% | 0 | 0 |

  10. | abc123 | 0 | 65% | 0 |

  11. | abc123 | 0 | 0 | 72% |

  12. | hjkk86 | 69% | 0 | 0 |

  13. | hjkk86 | 0 | 63% | 0 |

  14. | hjkk86 | 0 | 0 | 74% |

  15. +----------+---------+--------+------+

第二步 按class_id分组,求最大值(即去掉0)

 
  1. select class_id,

  2. max(case when grade = 'primary' then rate else 0 end) as 'primary',

  3. max(case when grade = 'middle' then rate else 0 end) as 'middle',

  4. max(case when grade = 'high' then rate else 0 end) as 'high'

  5. from mst_class

  6. group by class_id;

  7. +----------+---------+--------+------+

  8. | class_id | primary | middle | high |

  9. +----------+---------+--------+------+

  10. | abc123 | 70% | 65% | 72% |

  11. | hjkk86 | 69% | 63% | 74% |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值