mysql行转列的问题,但是不是并接?
发布网友
发布时间:2022-04-22 08:00
我来回答
共1个回答
热心网友
时间:2023-09-19 13:31
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) f2、用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视频教程