问答详情
源自:2-4 MySQL-使用CASE方法实现行转列

执行结果行中有NULL

select  sum(case when a.user_id=1 then b.kills end) as 'wukong',

sum(case when a.user_id=2 then b.kills end) as 'bajie',

sum(case when a.user_id=3 then b.kills end) as 'shaseng'

from user a join user_kills b on a.user_id = b.user_id group by b.user_id;




结果如下:

+--------+-------+---------+

| wukong | bajie | shaseng |

+--------+-------+---------+

|  50722 |  NULL |    NULL |

|   NULL | 15310 |    NULL |

|   NULL |  NULL |    2100 |

+--------+-------+---------+

3 rows in set (0.00 sec)


提问者:hwbaker 2018-01-26 15:59

个回答

  • 慕粉2213527730
    2018-02-01 18:18:49

    把group by 那句删掉