MySQL行轉列 列轉行

2021-09-19 03:53:14 字數 978 閱讀 7301

max(case course when '數學' then score else 0 end ) 數學,

max(case course when '語文' then score else 0 end ) 語文,

max(case course when '英語' then score else 0 end ) 英語

from test_tb_grade

group by user_name;

select user_name, '語文' course , cn_score as score from test_tb_grade

union select user_name, '數學' course, math_score as score from test_tb_grade

union select user_name, '英語' course, en_score as score from test_tb_grade

order by user_name,course;

非**至於為什麼要使用max()等聚合函式呢?若不用,沒order by之前的語句執行結果:

這樣看來,你還能不用嗎 ?

列轉行:

Mysql 行轉列 列轉行

create table test tb grade id int 10 not null auto increment,user name varchar 20 default null,course varchar 20 default null,score float default 0 pr...

MySQL 行轉列 列轉行

以下內容包括 行轉列 sql 執行效果展示 列轉行 sql 執行效果展示 總結 附上的建表語句 一 行轉列 廢話不多說,先上sql 兩種方式 行轉列 方法 select id,name,group concat case when subject 語文 then score end separato...

mysql列邊行 mysql 行轉列 列轉行

group concat 函式說明 手冊上說明 該函式返回帶有來自乙個組的連線的非null值的字串結果 通俗點理解,其實是這樣的 group concat 會計算哪些行屬於同一組,將屬於同一組的列顯示出來。要返回哪些列,由函式引數 就是欄位名 決定。分組必須有個標準,就是根據group by指定的列...