首頁 >資料庫 >mysql教程 >mysql如何實作行轉列

mysql如何實作行轉列

coldplay.xixi
coldplay.xixi原創
2020-11-23 09:14:445314瀏覽

mysql實現行轉列的方法:1、用cross join的方式實現,程式碼為【cross join(select sum(a.kills) '豬八戒】;2、用case語句的方式實現,程式碼為【case when user_name='孫悟空'】。

mysql如何實作行轉列

#本教學操作環境: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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

相關文章

看更多