数据库行列转换

源:http://wenku.baidu.com/view/5dda306127d3240c8447efb1.html

评:

<!--[if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->

要求查询出的数据:

mysql> create table student (id varchar(50), name varchar(50), subject varchar(5
0), result int, primary key (id));

 

 

插入数据:
insert into student values(‘1’,'zhangsan','yuwen',80)
insert into student values(‘2’,'zhangsan','shuxue',70)
insert into student values(‘3’,'zhangsan','wuli',90)
insert into student values(‘4’,'lisi','yuwen',75)
insert into student values(‘5’,'lisi','shuxue',84)
insert into student values(‘6’,'lisi','wuli',69)

 

<!--[if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->

**************   查询语句   **************

 

select name, max(case subject when 'shuxue' then  else 0 end) shuxue, max(case subject when 'yuwen' then  else 0 end) yuwen, max(case subject when 'wuli' then  else 0 end) wuli from student group by name;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值