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如何實作行轉列的詳細內容。更多資訊請關注PHP中文網其他相關文章!