当前位置:   article > 正文

SQL之CASE WHEN用法详解

SQL之CASE WHEN用法详解

一、CASE WHEN基础用法详解

语法格式:

CASE WHEN condition THEN result

[WHEN...THEN...] ELSE result

END

  1. SELECT
  2. STUDENT_NAME,
  3. (CASE WHEN score < 60 THEN '不及格'
  4. WHEN score >= 60 AND score < 80 THEN '及格'
  5. WHEN score >= 80 THEN '优秀'
  6. ELSE '异常' END) AS REMARK
  7. FROM
  8. TABLE

二、where语句后跟case语句

  1. select *
  2. from A, B
  3. where
  4. CASE
  5. when A.id = 0 then B.name in ('aaa', 'bbb')
  6. when A.id = 1 then B.name in ('bbb', 'ccc')
  7. when A.id = 2 then B.name in ('ccc', 'ddd')
  8. END

三、Update语句使用case更新

  1. update t_salary
  2. set
  3. salary =
  4. (
  5. case
  6. when salary < 3000 then salary + salary * 0.2
  7. when salary >= 3000 then salary + salary * 0.08
  8. else salary
  9. end
  10. )

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/452816
推荐阅读
相关标签
  

闽ICP备14008679号