mysql數據庫列轉行,mysql then_mysql語法之case when then與列轉行

 2023-10-21 阅读 27 评论 0

摘要:mysql語法中case when then與列轉行的使用場景非常豐富。case語句類似java中條件分支語句的作用,可以類比java中的switch語句或者if語句來學習。其語法如下:mysql數據庫列轉行。case語句的語法:轉換單個字段時:case?字段when?字段值?then?返回值M

mysql語法中case when then與列轉行的使用場景非常豐富。

case語句類似java中條件分支語句的作用,可以類比java中的switch語句或者if語句來學習。

其語法如下:

mysql數據庫列轉行。case語句的語法:

轉換單個字段時:

case?字段

when?字段值?then?返回值

MySQL 主從。when?字段值?then?返回值

when?字段值?then?返回值

。。。。。。

else?默認值?end 別名;

MySQL substring,當有多個字段需要轉換時:

case

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

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

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

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

。。。。。。

else?默認值?end 別名;

MySQL select、而列轉行則能夠幫我們把數據庫某些列轉換為行的形式展示給我們。

例如:

82645998dc57397f107b1e5d970ee564.png

將上述結果進行列轉行可以得到如下結果:

7aeea3dacb31fa0f0cd89bb23f341623.png

列轉行語法:

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

創建一個數據庫stu,建立學生表,班級表,課程表,成績表

班級表包含班級id、班級名稱

創建班級表:

mysql,set foreign_key_checks=0;

drop table if exists `classes`;

create table `classes` (

`cid` int(11) not null,

case when 多條件、`cname` varchar(10) default null,

primary key (`cid`)

) engine=innodb default charset=utf8mb4;

向班級表插入一個班級

mysql列轉行的方法、insert into `classes` values ('111', '一班');

課程表包含課程id、課程名稱

創建課程表:

drop table if exists `sc`;

case when then語法。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

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/158601.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息