日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

1.一維轉(zhuǎn)二維

mysql行列轉(zhuǎn)換

 

上圖為成績表中數(shù)據(jù),現(xiàn)希望將數(shù)據(jù)轉(zhuǎn)換為下圖。

mysql行列轉(zhuǎn)換

 

①靜態(tài):轉(zhuǎn)化為二維表后的列名及列數(shù)是確定不變的,本例中即course只有數(shù)學、語文、英語這三門課。

select s_name,
max(if(course="數(shù)學",score,0)) as 數(shù)學,
max(if(course='語文',score,0)) as 語文,
max(if(course='英語',score,0)) as 英語,
sum(score) as 總分
from grade group by s_name;
mysql行列轉(zhuǎn)換

 

②動態(tài):轉(zhuǎn)化為二維表后的列名及列數(shù)是可變的,本例中即course的課程數(shù)不確定。

set @sql='';
select@sql:=concat(@sql,'max(if(course='',course,'',score,0)) as ',course,',')from (select distinct course from grade) as a;
set@strsql=concat('select s_name,',@sql,'sum(score)as 總分 from grade group by s_name;');
prepare stmt from @strsql;
execute stmt;
deallocate prepare stmt;
mysql行列轉(zhuǎn)換

 

2.二維轉(zhuǎn)一維

mysql行列轉(zhuǎn)換

 

上圖為成績表2中數(shù)據(jù),現(xiàn)希望將數(shù)據(jù)轉(zhuǎn)為成績表1的數(shù)據(jù)。

mysql行列轉(zhuǎn)換

 

select name,'數(shù)學'as course,數(shù)學 as score from grade2
union all
select name,'語文'as course,語文 as score from grade2
union all
select name,'英語'as course,英語 as score from grade2
order by name;
mysql行列轉(zhuǎn)換

 

分享到:
標簽:mysql
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定