mysql行转列查询

    

为了完成行转列的查询 直接写最终结果

select

 sum(case compared_date when '1' then oneLine else 0),

 sum(case compared_date when '2' then oneLine else 0),

 sum(case compared_date when '3' then oneLine else 0)

from 表名

        where 条件



样例:网上比较多的一个课程老师学生的例子

1. 创建表

 create table exampleTable (course varchar(20),teacher varchar(20),student varchar(20),score  int) ;

2.插入数据

insert into exampleTable (course,teacher,student,score) values ('数学',' 王华','丽丽',60);

insert into exampleTable (course,teacher,student,score) values ('数学',' 王华','李四',30);

insert into exampleTable (course,teacher,student,score) values ('语文',' 张三','丽丽',70);

insert into exampleTable (course,teacher,student,score) values ('语文',' 张三','李四',80);

3.查询结果


4.现在查询学生李四和丽丽的数学成绩和语文成绩


select student,

sum(case course when '数学' then score else 0 end) as '数学' ,

sum(case course when '语文' then score else 0 end) as '语文'

from exampleTable group by student;


查询结果






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值