mysql怎么做纵表_MySQL横纵表相互转化操作实现⽅法本⽂实例讲述了MySQL横纵表相互转化操作实现⽅法。分享给⼤家供⼤家参考,具体如下:
先创建⼀个成绩表(纵表)
xml格式全称create table user_score
(
name varchar(20),
subjects varchar(20),
帝国cms恢复首页score int
);
insert into user_score(name,subjects,score) values('张三','语⽂',60);
insert into user_score(name,subjects,score) values('张三','数学',70);
insert into user_score(name,subjects,score) values('张三','英语',80);
insert into user_score(name,subjects,score) values('李四','语⽂',90);
android注册登录实例insert into user_score(name,subjects,score) values('李四','数学',100);
再创建⼀个成绩表(横表)
create table user_score2
(
name varchar(20),
yuwen int,
shuxue int,
yingyu int
);
insert into user_score2(name,yuwen,shuxue,yingyu) values('张三',60,70,80);
insert into user_score2(name,yuwen,shuxue,yingyu) values('李四',90,100,0);
java基础教程免费阅读纵表转横表
mysql怎么读英语select name,sum(case subjects when '语⽂' then score else 0 end)
as '语⽂',sum(case subjects when '数学' then score else 0 end)
as '数学', sum(case subjects when '英语' then score else 0 end)
as '英语'from user_score group by name;
纵表转横表
SELECT name,'yuwen' AS subjects,yuwen AS score FROM user_score2 UNION ALL SELECT name,'shuxue' AS subjects,shuxue AS score FROM user_score2 UNION ALL SELECT name,'yingyu' AS subjects,yingyu AS score FROM user_score2
ORDER BY name,subjects DESC;
电子个人简历表格手机版希望本⽂所述对⼤家MySQL数据库计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论