mysql then_mysql语法之case when then与列转行

mysql语法中case when then与列转行的使用场景非常丰富。

case语句类似java中条件分支语句的作用,可以类比java中的switch语句或者if语句来学习。

其语法如下:

case语句的语法:

转换单个字段时:

case 字段

when 字段值 then 返回值

when 字段值 then 返回值

when 字段值 then 返回值

。。。。。。

else 默认值 end 别名;

当有多个字段需要转换时:

case

when 字段名1=字段值11 and字段名2=字段值2 and。。。then返回值

when 字段名1=字段值12 and字段名2=字段值3 and。。。then返回值

when 字段名1=字段值13 and字段名2=字段值4 and。。。then返回值

when 字段名1=字段值14 and字段名2=字段值5 and。。。then返回值

。。。。。。

else 默认值 end 别名;

而列转行则能够帮我们把数据库某些列转换为行的形式展示给我们。

例如:

82645998dc57397f107b1e5d970ee564.png

将上述结果进行列转行可以得到如下结果:

7aeea3dacb31fa0f0cd89bb23f341623.png

列转行语法:

sum(case when then else end) as 或者max(case when then else end) as

创建一个数据库stu,建立学生表,班级表,课程表,成绩表

班级表包含班级id、班级名称

创建班级表:

set foreign_key_checks=0;

drop table if exists `classes`;

create table `classes` (

`cid` int(11) not null,

`cname` varchar(10) default null,

primary key (`cid`)

) engine=innodb default charset=utf8mb4;

向班级表插入一个班级

insert into `classes` values ('111', '一班');

课程表包含课程id、课程名称

创建课程表:

drop table if exists `sc`;

create table `sc` (

`scid` int(11) not null,

`scname` varchar(10) default null,

primary key (`scid`)

) engine=innodb default charset=utf8mb4;

向课程表插入三门课程

insert into `sc` values ('11', '语文');

insert into `sc` values ('22', '数学');

insert into `sc` values ('33', '英语');

成绩表包含成绩id、分数、分数所属学生id、分数所属课程id

创建成绩表:

drop table if exists `score`;

create table `score` (

`coid` int(11) not null,

`score` int(8) default null,

`sid` int(11) default null,

`scid` int(11) default null,

primary key (`coid`)

) engine=innodb default charset=utf8mb4;

向成绩表插入6条记录:

insert into `score` values ('1111', '90', '1', '11');

insert into `score` values ('2222', '99', '1', '22');

insert into `score` values ('3333', '89', '2', '11');

insert into `score` values ('4444', '88', '2', '33');

insert into `score` values ('5555', '75', '3', '22');

insert into `score` values ('6666', '59', '3', '33');

学生表包含学生id、学生姓名、所属班级id

创建学生表:

drop table if exists `student`;

create table `student` (

`sid` int(11) not null,

`sname` varchar(10) default null,

`cid` int(11) default null,

primary key (`sid`)

) engine=innodb default charset=utf8mb4;

向学生表插入三个学生:

insert into `student` values ('1', '张三', '111');

insert into `student` values ('2', '李四', '111');

insert into `student` values ('3', '王五', '111');

查询每个学生的姓名、所属班级、所学课程、课程得分

select

s.sname,

cl.cname,

sc.scname,

co.score

from

student s,

classes cl,

sc,

score co

where

s.cid = cl.cid

and s.sid = co.sid

and sc.scid = co.scid;

82645998dc57397f107b1e5d970ee564.png

在上一个结果集的基础上,将同一个学生的所有所学课程与所有分数分别以逗号形式连接成字符串:

select

s.sname,

cl.cname,

group_concat(co.score) 分数,

group_concat(sc.scname) 课程

from

student s,

classes cl,

sc,

score co

where

s.cid = cl.cid

and s.sid = co.sid

and sc.scid = co.scid

group by

s.sname,

cl.cname;

b1631406a98494cc372b2643e8af564e.png

列转行实例:

select

s.sname,

cl.cname,

ifnull(

max(

case sc.scname

when '语文' then

co.score

end

),

'未选'

) as '语文',

ifnull(

max(

case sc.scname

when '数学' then

co.score

end

),

'未选'

) as '数学',

ifnull(

max(

case sc.scname

when '英语' then

co.score

end

),

'未选'

) as '英语'

from

student s,

classes cl,

sc,

score co

where

s.cid = cl.cid

and s.sid = co.sid

and sc.scid = co.scid

group by

s.sname,

cl.cname;

7aeea3dacb31fa0f0cd89bb23f341623.png

select

s.sname,

cl.cname,

max(

if (

sc.scname = '语文',

co.score ,- 1

)

) as '语文',

max(

if (

sc.scname = '数学',

co.score ,- 1

)

) as '数学',

max(

if (

sc.scname = '英语',

co.score ,- 1

)

) as '英语'

from

student s,

classes cl,

sc,

score co

where

s.cid = cl.cid

and s.sid = co.sid

and sc.scid = co.scid

group by

s.sname,

cl.cname;

ffbe885903abaf429478c15a232a3399.png

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值