mysql如何实现行转列

MysqL实现行转列的方法:1、用cross join的方式实现,代码为【cross join(select sum(a.kills) '猪八戒】;2、用case语句的方式实现,代码为【case when user_name='孙悟空'】。

本教程操作环境:windows7系统、MysqL8.0.22版,该方法适用于所有品牌电脑。

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视频教程

相关文章

目录MySQL卸载环境查看是否已安装MySQL卸载mysql服务查看是否...
目录数据类型数据类型分类数值类型以TINYINT认识整型族有符号...
目录表的约束空属性非空约束(NOT NULL Constraint)默认值定...
目录函数时间日期函数:字符串函数数学函数其他函数 函数 时间...
目录使用C语言连接库的安装C APImysql_initmysql_real_conne...
目录用户用户管理查询所有用户查看当前用户查看当前连接数创...