当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql语法之case when then与列转行

mysql语法之case when then与列转行

2019年04月23日  | 移动技术网IT编程  | 我要评论

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 别名;

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

例如:

 

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

 

列转行语法:

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;

 

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

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;

 

 

列转行实例:

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;

 

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;

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网