--两种用法 select case when 字段1 = 值1 then 值1+10 else 值1-10 end case 字段1 when 值1 then 值1+10 else 值1-10 end from your_table
--上面好了一个逗号 --两种用法 select case when 字段1 = 值1 then 值1+10 else 值1-10 end ,case 字段1 when 值1 then 值1+10 else 值1-10 end from your_table--when可以有多个 --两种用法 select case when 字段1 = 值1 then 字段1 + 10 when 字段1 = 值2 then 字段1 + 20 when 字段1 = 值3 then 字段1 + 30 else 字段1 - 10 end, case 字段1 when 值1 then 字段1 + 10 when 值2 then 字段1 + 20 when 值3 then 字段1 + 30 else 字段1 - 10 end from your_table
想想C语言里面的case就知道了,意思是一样的 比如表里有个字段:状态,而状态只会有三种:1/2/3,分别代表'默认值','成功','失败' 现在要统计这三种状态在表中各自的总数,就要用到case了 select case 状态 when 1 then '默认值' when 2 then '成功' when 3 then '失败' end ,count(1) from your_table group by 状态 /* --结果 状态|总数 默认值|100 成功|90 失败|80*/
--两种用法
select case when 字段1 = 值1 then 值1+10 else 值1-10 end
case 字段1 when 值1 then 值1+10 else 值1-10 end
from your_table
--上面好了一个逗号
--两种用法
select case when 字段1 = 值1 then 值1+10 else 值1-10 end
,case 字段1 when 值1 then 值1+10 else 值1-10 end
from your_table--when可以有多个
--两种用法
select case when 字段1 = 值1 then 字段1 + 10
when 字段1 = 值2 then 字段1 + 20
when 字段1 = 值3 then 字段1 + 30
else 字段1 - 10
end,
case 字段1 when 值1 then 字段1 + 10
when 值2 then 字段1 + 20
when 值3 then 字段1 + 30
else 字段1 - 10
end
from your_table
或者说实现一个功能,用case是最简单的。不知道什么时候用
想想C语言里面的case就知道了,意思是一样的
比如表里有个字段:状态,而状态只会有三种:1/2/3,分别代表'默认值','成功','失败'
现在要统计这三种状态在表中各自的总数,就要用到case了
select case 状态 when 1 then '默认值'
when 2 then '成功'
when 3 then '失败'
end
,count(1)
from your_table
group by 状态
/*
--结果
状态|总数
默认值|100
成功|90
失败|80*/