学生成绩表,列转行

创建表:

create table t_test
(
id int not null AUTO_INCREMENT,
tName varchar(30) not null collate gbk_chinese_ci,
tKe varchar(20) not null collate gbk_chinese_ci,
tPoint int,
primary key(id)
);

表数据:

insert into t_test(tName,tKe,tPoint)
values
('jacky','chinese',30),
('jacky','english',78),
('caiin','chinese',10),
('caiin','english',65);

列转行:

SELECT tName,max(case tKe when 'chinese' then tPoint else 0 end) AS '语文',
 max(case tKe when 'english' then tPoint else 0 end) as '英语'
  from fd.t_test group by tName;

原文地址:https://www.cnblogs.com/jackychua/p/2986317.html