mysql 将列值转变为列的方法
程序员文章站
2024-02-22 21:05:10
复制代码 代码如下:-- 创建库create table `rate` ( `uname` varchar (300), `object` varcha...
复制代码 代码如下:
-- 创建库
create table `rate` (
`uname` varchar (300),
`object` varchar (300),
`score` varchar (300)
);
-- 插入数据
insert into test.rate (uname, object, score)
values('aaa', 'chinese', '67'),
('aaa', 'math', '89'),
('aaa', 'physical', '89'),
('bbb', 'chinese', '67'),
('bbb', 'math', '75'),
('bbb', 'physical', '89');
-- 查询
select distinct uname as '姓名',
sum(case object
when 'chinese' then score end) as '语文',
sum(case object
when 'math' then score end) as '数学',
sum(case object
when 'physical' then score end) as '物理' from rate group by uname;
上一篇: Android 绘制太极图实例详解