mysql中case when then end的含义和用法

mac2022-06-30  23

参见这篇博客:https://blog.csdn.net/rongtaoup/article/details/82183743 case when then else end 语句用于查询满足多种条件的情况,类似java中的if...else,还有的就是用于进行行转列的查询,这个是放在select 子句后面的,是充当的是字段的作用。 具体用法就是:分为两种,一种是简单的函数形式,另一种就是表达式的形式。 其含义解释: 简单的函数形式 case 字段 when 值 then 结果 else 其他情况 end;

表达式的形式 case when 字段=值(这里写表达式,例如 score=80) then 结果 else 其他情况 end;

简单的函数形式 select case score when 'a' then '优秀' else '良好' end from student; 表达式形式 select case when score between and 90 then '优秀' else '良好' end from student;

在case里面还可以嵌套子查询等复杂用法。 case when 可以把它当成一个字段,其实就是select 后面需要查询出来的一个字段,对其可以使用聚合函数,别名,去重,等操作。

转载于:https://www.cnblogs.com/jasonboren/p/11575476.html

最新回复(0)