这篇文章主要介绍mysql实现行转列的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
mysql实现行转列的方法:1、用cross join的方式实现,代码为【cross join(select sum(a.kills) '猪八戒】;2、用case语句的方式实现,代码为【case when user_name='孙悟空'】。
mysql实现行转列的方法:
1、用cross join的方式实现
select * from (select sum(a.kills) '孙悟空' from kills1 as a LEFT JOIN tssrz as b on a.user_id = b.id WHERE b.user_name = '孙悟空' GROUP BY b.user_name) e cross join (select sum(a.kills) '猪八戒' from kills1 as a LEFT JOIN tssrz as b on a.user_id = b.id WHERE b.user_name = '猪八戒' GROUP BY b.user_name) f
2、用case语句的方式实现
SELECT sum(case when user_name='孙悟空' then kills end) as '孙悟空', sum(case when user_name='猪八戒' then kills END) as '猪八戒', sum(case when user_name='沙和尚' then kills END) as '沙和尚' from tssrz as a join kills1 as b on a.id = b.user_id
以上是“mysql实现行转列的方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。