关于mysql 对 行转列,列转行的sql 编写,只用case when的写法
现有一组数据如下,该组数据网上随便找的,具体参考业务需求
利用SQL达到如下效果
实现语句
select name,max(case when subject=‘chinese’ then score end )chinese,max(case when subject=‘english’ then score end) english,max(case when subject=‘math’ then score end) math from hl group by name;
注意此处为什么要加max()函数,是因为我们case when后 没有匹配到的地方会以null值呈现,就像下面这种情况(没用group by是方便大家理解)
所以用max取每列的最大值(即有数据的那一行)
如果反过来的话该怎么样处理呢