博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MYSQL列转行操作
阅读量:7033 次
发布时间:2019-06-28

本文共 1156 字,大约阅读时间需要 3 分钟。

  hot3.png

select a.student,

max(if(a.course='english',a.score,0)) as english,
max(if(a.course='computer',a.score,0)) as 'computer',
max(if(a.course='history',a.score,0)) as 'history',
max(if(a.course='math',a.score,0)) as 'math'
from scores a
group by a.student;

 

SET @EE='';

SELECT  :=CONCAT(,'MAX(IF(course=\'',course,'\',score,0)) AS ',course,',') FROM (SELECT DISTINCT course FROM scores) A;
select :=concat('select ',,'student from scores group by student') from dual;
prepare stmt from @QQ;
execute stmt;

附测试数据

create table scores(

 id int primary key,
 student char(20),
 course char(20),
 score int
);

 

insert into scores values

(1 ,'Leilei','english',9),
(2 ,'Jim','english',7),
(3 ,'Tom','english',4),
(4 ,'John','english',2),
(5 ,'Leilei','computer',2),
(6 ,'Jim','computer',9),
(7 ,'Tom','computer',8),
(8 ,'John','computer',5),
(9 ,'Leilei','history',1),
(10 ,'Jim','history',8),
(11 ,'Tom','history',8),
(12 ,'John','history',6),
(13 ,'Leilei','math',8),
(14 ,'Jim','math',2),
(15 ,'Tom','math',6),
(16 ,'John','math',9),
(17 ,'Leilei','math',3),
(18 ,'Jim','math',5),
(19 ,'Tom','math',2),
(20 ,'John','math',5);

 

转载于:https://my.oschina.net/haison/blog/733003

你可能感兴趣的文章
关于JQuery Class选择器的一点
查看>>
POJ3264 Balanced Lineup
查看>>
redis-cli 连接远程服务器
查看>>
emlog通过pjax实现无刷新加载网页--完美解决cnzz统计和javascript失效问题
查看>>
sublime 之 vitage/emmet
查看>>
代码管理(四)SVN和Git对比
查看>>
python - hadoop,mapreduce demo
查看>>
mongodb常见管理命令
查看>>
1.7 以函数对象取代函数
查看>>
Vue过渡效果之JS过渡
查看>>
Android项目实战(三):实现第一次进入软件的引导页
查看>>
Web Service基础——基础概念
查看>>
Linux2.4文件系统中vfsmount、安装点的dentry、设备的dentry之间的关系【转】
查看>>
POJ 1201 Intervals
查看>>
JAVA訪问URL
查看>>
APP接口基础学习一
查看>>
设计模式 策略模式 以角色游戏为背景
查看>>
【转】CSS和SVG中的剪切——clip-path属性和<clipPath>元素
查看>>
【C语言入门教程】5.4 递归
查看>>
UVALive 6915 Leveling Ground 倍增RMQ
查看>>