MySQL乙個類行列轉置的顯示處理

2021-10-01 13:45:29 字數 1424 閱讀 1169

首先,建立表return_change:

插入符合表字段型別屬性的資料,如上例子

直接語句:

selectname, case cname when '語文' then score else '' end '語文', case cname when '數學' then score else '' end '數學', case cname when '物理' then score else '' end'物理' from return_change;

這是最開始的結果,不難發現此時的資料比原來的更醜,所以設法進行處理空格問題,(這裡是根據條件進行匹配,然後輸出,所以仍舊會有三行資料,別的行就會為空行),如下為結果:

selectname, max(case cname when '語文' then score else '' end) '語文', max(case cname when '數學' then score else '' end) '數學', max(case cname when '物理' then score else '' end)'物理' from return_change

但是這裡會只顯示乙個人的記錄,所以還需要更改,使用分組顯示所有的;

最後**為:

selectname, max(case cname when '語文' then score else '' end) '語文', max(case cname when '數學' then score else '' end) '數學', max(case cname when '物理' then score else '' end)'物理' from return_change group byname;

結果如下圖:

如有不妥,還請指正!

乙個sql的行列轉置的例子。

有這樣乙個aaa表如下所示 name score color jim 10 red jim 20 blue jim 20 green jim 1 black glin 2 red glin 33 blue glin 21 green glin 19 black bob 22 red bob 39 b...

秒懂mysql語句的行 列轉置

1.原圖 轉為 圖形放錯位置了,請注意 資料 create table stu score grade id varchar 10 default null,subject name varchar 10 default null,max score int 10 default null inse...

寫一函式,將乙個3 3的整型矩陣轉置。

寫一函式,將乙個3 3的整型矩陣轉置。指標法 方法一 include void trans int s 3 int x,int y 定義轉置函式 intmain 轉置函式 void trans int s 3 int x,int y 方法二 利用動態記憶體分配 include include voi...