在sql中case子句的两种形式
时间:2021-07-01 10:21:17
帮助过:32人阅读
select *
from SDetail
--
插入初始数据
insert into SDetail (SName,SScore,SGender) values(‘刘德华‘,
99,
0)
insert into SDetail (SName,SScore,SGender) values(‘古天乐‘,
55,
0)
insert into SDetail (SName,SScore,SGender) values(‘张惠妹‘,
55,
1)
insert into SDetail e(SName,SScore,SGender) values(‘梁家栋‘,
43,
0)
insert into SDetail (SName,SScore,SGender) values(‘王力宏‘,
99,
0)
insert into SDetail (SName,SScore,SGender) values(‘刘亦菲‘,
88,
1)
View Code
case 子句判断相等
--case 语法的两种形式
select * ,
case SGender when 0 then ‘男‘ when 1 then ‘女‘ end as ‘性别‘
from SDetail
View Code
执行结果:
二、判断不等(范围)
case when 包含列名的逻辑表达式 then ...
when ... then ...
else ...
end as 列别名
--case 判断不等(范围)
select * ,
case when SScore>60 then ‘及格‘ else ‘不及格‘ end as ‘是否及格‘
from SDetail
View Code
执行结果:
在sql中case子句的两种形式
标签:bsp else core 刘德华 case logs insert val display