Mysql之如何實現(xiàn)行列轉(zhuǎn)換
mysql數(shù)據(jù)庫如何實現(xiàn)行列轉(zhuǎn)換
1. 行轉(zhuǎn)列
方案一
select name, sum(case when course='java' then grade end) as java, sum(case when course='C++' then grade end) as C++, sum(case when course='C#' then grade end) as C# from test group by name
方案二
select distinct c.`name` AS name, (select grade from test where name = c.`name` and course = 'java' )as java, (select grade from test where name = c.`name` and course = 'C++' )as C++, (select grade from test where name = c.`name` and course = 'C#' )as C# from test c
結(jié)合項目用到的sql:
select MAIN_STATION_CODE_ as stationCode, case when SUBSTR(PLAN_START_DATE_, 1, 10)=curdate() then STATION_ else "" end as firstDay, case when SUBSTR(PLAN_START_DATE_, 1, 10)=DATE_SUB(curdate(),INTERVAL -1 DAY) then STATION_ else "" end as secondDay, case when SUBSTR(PLAN_START_DATE_, 1, 10)=DATE_SUB(curdate(),INTERVAL -2 DAY) then STATION_ else "" end as thirdDay, case when SUBSTR(PLAN_START_DATE_, 1, 10)=DATE_SUB(curdate(),INTERVAL -3 DAY) then STATION_ else "" end as fourthDay from ps_overhaul_plan_row group by MAIN_STATION_CODE_
方案二拓展
上面我們是采用逐個判斷并拼接的方式來進行,那如果極端情況下,假如有成百上千個值需要判斷怎么辦?
這種情況下,能夠立即想到的是采用函數(shù)來拼接出對應的語句塊,再合并在一起執(zhí)行。
假如我們需要拼接下面一句:
(select GRADE_ FROM grade where name_ = c.name_ and course_ = ‘Math') as Math,
使用concat函數(shù)(例子):
select c.name_,concat('(SELECT grade_ from grade where name_=c.name_ ', 'and course_ =''', c.course_, '''', ') as ',c.course_, ')') from grade c
然后再次使用group_concat函數(shù)將多行轉(zhuǎn)為一行:
SELECT GROUP_CONCAT(distinct concat('(SELECT grade_ from grade where name_=c.name_ ', 'and course_ =''', c.course_, '''', ') as ',c.course_, ')')) from grade c
最后再使用存儲過程完成動態(tài)sql執(zhí)行。
方案三:帶匯總
select ifnull(uid,'Total') uid, uname, sum(if(`course`='java',grade,0)) 'java', sum(if(`course`='C++',grade,0)) 'C++', sum(if(`course`='C#',grade,0)) 'C#', sum(score) 'total' from course group by uid with ROLLUP
方案四:使用group_concat函數(shù)
SELECT id, name, group_concat(CASE WHEN subject = 'Math' THEN score END SEPARATOR '') '數(shù)學', group_concat(CASE WHEN subject = 'English' THEN score END SEPARATOR '') '英語' FROM test1 GROUP BY name;
2. 列轉(zhuǎn)行
原表:
實現(xiàn)SQL:
SELECT d.name_,'Math' AS subject,d.Math_ AS score FROM grade_column d UNION ALL SELECT d.name_, 'English' as SUBJECT,d.English_ as score from grade_column d
列轉(zhuǎn)行效果如下:
說明:SELECT “hello” as subject from dual;作用是新添加列subject,并且列值為hello;
總結(jié)
行轉(zhuǎn)列原理:
CASE WHEN或IF,這兩種都是判斷條件,滿足條件的時候我們把它當做新的一列。
列轉(zhuǎn)行原理:
UNION或UNION ALL,這兩個都是把結(jié)果集合并起來,每次查詢學生名稱(基本列)和學科的其中一列的值,再把它們組合起來,這樣結(jié)果集就只有學生名稱和科目成績兩列了,這里多加了一列科目。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL關(guān)鍵字IN與EXISTS的使用與區(qū)別詳解
in和exists是兩種常用的條件查詢關(guān)鍵字,兩種常用于子查詢,它們在某些情況下可以互換使用,但它們的工作方式和效率可能會有所不同,這篇文章主要給大家介紹了關(guān)于MySQL關(guān)鍵字IN與EXISTS的使用與區(qū)別的相關(guān)資料,需要的朋友可以參考下2024-09-09解決MySQL登錄報錯1045-Access?denied?for?user?'root'@
這篇文章主要給大家介紹了關(guān)于解決MySQL登錄報錯1045-Access?denied?for?user?‘root‘@‘‘(using?password:YES)的相關(guān)資料,文中一步步將解決的辦法介紹的非常詳細,需要的朋友可以參考下2023-07-07