如何转换它:
name sub value
a maths 4
b science 5
a history 3
c computer1
c english 4
b computer5
c history 3
a science 8
b maths 3
变成这样:
name maths science history computer english
a 4 8 3 5 NIL
b 3 5 NIL NIL NIL
c NIL NIL 3 1 4
我已经做了这样的查询。
select @cols := group_concat(distinct replace('MAX(CASE WHEN sub = ''[val]'' then ''y'' ELSE ''n'' END) AS `[val]`', '[val]', sub))
from (select distinct sub from t) t;
set @sql = concat('select name, ', @cols,
' from t group by name'
);
prepare st from @sql;
execute st;
deallocate prepare st;
在此代码中,我使用了y或n个值。但是我需要将其替换为特定列值的相应值