56,687
社区成员
发帖
与我相关
我的任务
分享
GROUP_CONCAT( DISTINCT CONCAT('IFNULL(MAX(CASE WHEN pro_t.func_code = ''',pro_t.func_code,''' THEN pro_t.goal END),0) AS ''',pro_t.func_code,''''))
drop table if exists Class;
Create table Class(Student nvarchar(2),Course nvarchar(2),Score int);
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85 ;
#方法1
set @sql='';
select group_concat('max(case when Course=''',Course,''' then Score else 0 end) as ',Course) INTO @sql from (select distinct Course from class ) as a;
set @sql=concat('select Student,',@sql,' from Class group by Student') ;
prepare stmt from @sql;
execute stmt ;
DEALLOCATE PREPARE stmt;