专题2:一篇搞懂case when所有使用场景

文章介绍了SQL查询语句中Casewhen的三种主要应用:将性别int类型转换为汉字显示,按成绩范围转化为等级显示,以及数据表列转行的操作。通过案例展示了如何使用Casewhen进行等值、范围条件判断和数据重组,帮助理解Casewhen在实际数据库操作中的运用。
摘要由CSDN通过智能技术生成

case when的使用情况一般有三种:等值转换、范围转换、列转行操作

一、 等值转换

在数据库中建表的时候总是会把用户的性别存储为int类型('0’为女,'1’为男),但是如何转换成汉字显示呢?

原始表数据:

-- a1
drop table if exists a1;
create table a1(
    id bigint default '0' not null comment '主键id',
    name char(100) default '' not null comment '姓名',
    sex int default '0' not null
);
insert into a1 values (1,'张三',0);
insert into a1 values (2,'李四',1);
insert into a1 values (3,'王五',1);
insert into a1 values (3,'赵六',0);

在这里插入图片描述

转换后结果:

select
	id,
	name ,
	(case sex 
	 when 0 then '女' 
	 else '男' end) as sex
from a1;
或者
select
	id,
	name ,
	(case 
	 when sex=0 then '女' 
	 else '男' end) as sex
from a1;

在这里插入图片描述
一般的case when语句都会比较长,最好添加小括号包起来,这样更容易阅读。


二、范围转换

有时会遇到这种情况,按照用户成绩显示优(90+)、良(80-90)、及格(60-80)、未及格(60-),这是一个分数的范围,要如何转换成汉字显示呢?

原始表数据:

-- a2
drop table if exists a2;
create table a2(
    id bigint default '0' not null comment '主键id',
    name char(100) default '0' not null comment '名字',
    score int default '0' not null comment '分数'
);
insert into a2 values (1,'张三',67);
insert into a2 values (2,'李四',83);
insert into a2 values (3,'王五',55);
insert into a2 values (3,'赵六',92);

在这里插入图片描述


转换后结果:

select 
	id,
	name ,
	(case 
	 when score>=90 then '优' 
	 when score>=80 then '良' 
	 when score>=60 then '及格' 
	 else '不及格' end) as level
from a2; 

在这里插入图片描述
注:此时,case后面不可以跟任何东西,因为case when就像一个switch case语句,如果在case后跟了东西,它会拿它跟when对比;如果case后写了=score,而when后面写了score>=90 ,而'score'等于'score>=90'吗? 显然不等于。


三、列转行操作

现在有原始表“学生成绩表”, 要如何按照图2显示出来呢?

原始表数据:

-- a3
drop table if exists a3;
create table a3(
    id bigint default '0' not null comment '主键id',
    name char(100) default '0' not null comment '名字',
		course char(100) default '0' not null comment '课程',
    score int default '0' not null comment '分数'
);
insert into a3 values (1,'张三','数学',98);
insert into a3 values (2,'张三','语文',89);
insert into a3 values (3,'张三','英语',77);
insert into a3 values (4,'李四','数学',79);
insert into a3 values (5,'李四','语文',86);
insert into a3 values (6,'李四','英语',92);

在这里插入图片描述


转换后结果:

1、首先按照科目分开,符合条件的设置分数,不符合的给置零。

select 
	name,
	(case when course='语文' then score else 0 end) as '语文',
	(case when course='数学' then score else 0 end) as '数学',
	(case when course='英语' then score else 0 end) as '英语'
from a3

在这里插入图片描述

2、然后按照名字group by,对分数求max

select 
	name,
	max(case when course='语文' then score else 0 end) as '语文',
	max(case when course='数学' then score else 0 end) as '数学',
	max(case when course='英语' then score else 0 end) as '英语'
from a3
group by name;

在这里插入图片描述

总结

以上就是今天要讲的内容,本文主要介绍了目前遇到的case when所有使用场景,包括:等值转换、范围转换、列转行操作,后续若遇到其他场景会继续更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛媛媛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值