码迷,mamicode.com
首页 > 数据库 > 详细

sql中列数据横着显示

时间:2015-04-21 14:24:43      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:


列数据横着显示:
CREATE TABLE StudenScore(stuname VARCHAR(25) , kc VARCHAR(25) , fs INT)
INSERT INTO StudenScore VALUES(‘张三‘ , ‘语文‘ , 74);
INSERT INTO StudenScore VALUES(‘张三‘ , ‘数学‘ , 83);
INSERT INTO StudenScore VALUES(‘张三‘ , ‘物理‘ , 93);
INSERT INTO StudenScore VALUES(‘李四‘ , ‘语文‘ , 74);
INSERT INTO StudenScore VALUES(‘李四‘ , ‘数学‘ , 84);
INSERT INTO StudenScore VALUES(‘李四‘ , ‘物理‘ , 94)

SELECT * FROM StudenScore

SELECT stuname AS 姓名 ,
MAX(CASE kc WHEN ‘语文‘ THEN fs ELSE 0 END) 语文,
MAX(CASE kc WHEN ‘数学‘ THEN fs ELSE 0 END) 数学,
MAX(CASE kc WHEN ‘物理‘ THEN fs ELSE 0 END) 物理
FROM StudenScore
GROUP BY stuname;

 

SELECT stuname AS 姓名 ,
SUM(CASE kc WHEN ‘语文‘ THEN fs ELSE 0 END) 语文,
SUM(CASE kc WHEN ‘数学‘ THEN fs ELSE 0 END) 数学,
SUM(CASE kc WHEN ‘物理‘ THEN fs ELSE 0 END) 物理
FROM StudenScore
GROUP BY stuname;

 

至于为MAX和SUM在此处两者没有区别,都能查处数据

sql中列数据横着显示

标签:

原文地址:http://www.cnblogs.com/bky-234/p/4444124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!