mysql 行變列(多行變成一行/多行合并成一行/多行合并成多列/合并行)

來源:互聯網
上載者:User

標籤:英語   sep   chinese   img   並行   src   分割   數學   group by   

資料庫結構

而我想讓同一個人的不同成績變成此人在這一行不同列上顯示出來,此時分為2中展現:

第一種展現----【多行變一列】(合并後的資料在同一列上):

sql如下:

 

select name ,group_concat(sore Separator ‘;‘) as score from stu group by name 

第二種展現----【多行變多列】(合并後的資料在不同列上):

 

sql如下:

 

SELECT name ,MAX(CASE type WHEN ‘數學‘ THEN score ELSE 0 END ) math,MAX(CASE type WHEN ‘英語‘ THEN score ELSE 0 END ) English ,MAX(CASE type WHEN ‘語文‘ THEN score ELSE 0 END ) Chinese FROM stu  GROUP BY name

-----------------------------------------------------------------------------------------------------可愛的分割線----------------------------------------------------------------------------------------------------

 

當然,在第一種情況中(顯示在一列),也有些其他的類似形式:

形式一:

sql如下:

 

select name ,group_concat(type,‘分數為:‘,score  Separator ‘; ‘) as score from stu group by name 


呵呵,當然 如果你很熟悉group_concat和concat的用法,你也做出如下形式:

 

其sql如下:

 

select name ,concat(name ,‘的分數為[‘,group_concat(type,‘分數為:‘,score  Separator ‘; ‘),‘]‘) as score from stu group by name 

mysql 行變列(多行變成一行/多行合并成一行/多行合并成多列/合并行)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.