mysql 行轉列 列轉行

1、行轉列函數

即將本來同一列下多行的不一樣內容做爲多個字段,輸出對應內容。spa

建表語句3d

DROP TABLE IF EXISTS tb_score;

CREATE TABLE tb_score(
    id INT(11) NOT NULL auto_increment,
    userid VARCHAR(20) NOT NULL COMMENT '用戶id',
    subject VARCHAR(20) COMMENT '科目',
    score DOUBLE COMMENT '成績',
    PRIMARY KEY(id)
)ENGINE = INNODB DEFAULT CHARSET = utf8;

插入數據code

INSERT INTO tb_score(userid,subject,score) VALUES ('001','語文',90);
INSERT INTO tb_score(userid,subject,score) VALUES ('001','數學',92);
INSERT INTO tb_score(userid,subject,score) VALUES ('001','英語',80);
INSERT INTO tb_score(userid,subject,score) VALUES ('002','語文',88);
INSERT INTO tb_score(userid,subject,score) VALUES ('002','數學',90);
INSERT INTO tb_score(userid,subject,score) VALUES ('002','英語',75.5);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','語文',70);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','數學',85);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','英語',90);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','政治',82);

查詢數據表中的內容(即轉換前的結果)blog

SELECT * FROM tb_score

先來看一下轉換後的結果:排序

能夠看出,這裏行轉列是將原來的subject字段的多行內容選出來,做爲結果集中的不一樣列,並根據userid進行分組顯示對應的score。rem

一、使用case...when....then 進行行轉列字符串

SELECT userid,
SUM(CASE `subject` WHEN '語文' THEN score ELSE 0 END) as '語文',
SUM(CASE `subject` WHEN '數學' THEN score ELSE 0 END) as '數學',
SUM(CASE `subject` WHEN '英語' THEN score ELSE 0 END) as '英語',
SUM(CASE `subject` WHEN '政治' THEN score ELSE 0 END) as '政治' 
FROM tb_score 
GROUP BY userid

二、使用IF() 進行行轉列:數學

SELECT userid,
SUM(IF(`subject`='語文',score,0)) as '語文',
SUM(IF(`subject`='數學',score,0)) as '數學',
SUM(IF(`subject`='英語',score,0)) as '英語',
SUM(IF(`subject`='政治',score,0)) as '政治' 
FROM tb_score 
GROUP BY userid

注意點:class

(1)SUM() 是爲了可以使用GROUP BY根據userid進行分組,由於每個userid對應的subject="語文"的記錄只有一條,因此SUM() 的值就等於對應那一條記錄的score的值。

假如userid ='001' and subject='語文' 的記錄有兩條,則此時SUM() 的值將會是這兩條記錄的和,同理,使用Max()的值將會是這兩條記錄裏面值最大的一個。可是正常狀況下,一個user對應一個subject只有一個分數,所以可使用SUM()、MAX()、MIN()、AVG()等聚合函數均可以達到行轉列的效果

(2)IF(`subject`='語文',score,0) 做爲條件,即對全部subject='語文'的記錄的score字段進行SUM()、MAX()、MIN()、AVG()操做,若是score沒有值則默認爲0。

三、利用SUM(IF()) 生成列 + WITH ROLLUP 生成彙總行,並利用 IFNULL將彙總行標題顯示爲Total

SELECT IFNULL(userid,'total') AS userid,
SUM(IF(`subject`='語文',score,0)) AS 語文,
SUM(IF(`subject`='數學',score,0)) AS 數學,
SUM(IF(`subject`='英語',score,0)) AS 英語,
SUM(IF(`subject`='政治',score,0)) AS 政治,
SUM(IF(`subject`='total',score,0)) AS total
FROM(
    SELECT userid,IFNULL(`subject`,'total') AS `subject`,SUM(score) AS score
    FROM tb_score
    GROUP BY userid,`subject`
    WITH ROLLUP
    HAVING userid IS NOT NULL
)AS A 
GROUP BY userid
WITH ROLLUP;

運行結果:

四、利用SUM(IF()) 生成列 + UNION 生成彙總行,並利用 IFNULL將彙總行標題顯示爲 Total

SELECT userid,
SUM(IF(`subject`='語文',score,0)) AS 語文,
SUM(IF(`subject`='數學',score,0)) AS 數學,
SUM(IF(`subject`='英語',score,0)) AS 英語,
SUM(IF(`subject`='政治',score,0)) AS 政治,
SUM(score) AS TOTAL 
FROM tb_score
GROUP BY userid
UNION
SELECT 'TOTAL',SUM(IF(`subject`='語文',score,0)) AS 語文,
SUM(IF(`subject`='數學',score,0)) AS 數學,
SUM(IF(`subject`='英語',score,0)) AS 英語,
SUM(IF(`subject`='政治',score,0)) AS 政治,
SUM(score) FROM tb_score

運行結果:

五、利用SUM(IF()) 生成列,直接生成結果再也不利用子查詢

SELECT IFNULL(userid,'TOTAL') AS userid,
SUM(IF(`subject`='語文',score,0)) AS 語文,
SUM(IF(`subject`='數學',score,0)) AS 數學,
SUM(IF(`subject`='英語',score,0)) AS 英語,
SUM(IF(`subject`='政治',score,0)) AS 政治,
SUM(score) AS TOTAL 
FROM tb_score
GROUP BY userid WITH ROLLUP;

運行結果:

六、動態,適用於列不肯定狀況

SET @EE='';
select @EE :=CONCAT(@EE,'sum(if(subject= \'',subject,'\',score,0)) as ',subject, ',') AS aa FROM (SELECT DISTINCT subject FROM tb_score) A ;

SET @QQ = CONCAT('select ifnull(userid,\'TOTAL\')as userid,',@EE,' sum(score) as TOTAL from tb_score group by userid WITH ROLLUP');
-- SELECT @QQ;

PREPARE stmt FROM @QQ;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

運行結果:

七、合併字段顯示:利用group_concat()

SELECT userid,GROUP_CONCAT(`subject`,":",score)AS 成績 FROM tb_score
GROUP BY userid

運行結果:

group_concat(),手冊上說明:該函數返回帶有來自一個組的鏈接的非NULL值的字符串結果。
比較抽象,難以理解。通俗點理解,實際上是這樣的:group_concat()會計算哪些行屬於同一組,將屬於同一組的列顯示出來。要返回哪些列,由函數參數(就是字段名)決定。分組必須有個標準,就是根據group by指定的列進行分組。

結論:group_concat()函數能夠很好的建屬於同一分組的多個行轉化爲一個列。

3、列轉行

建表語句:

CREATE TABLE tb_score1(
    id INT(11) NOT NULL auto_increment,
    userid VARCHAR(20) NOT NULL COMMENT '用戶id',
    cn_score DOUBLE COMMENT '語文成績',
    math_score DOUBLE COMMENT '數學成績',
    en_score DOUBLE COMMENT '英語成績',
    po_score DOUBLE COMMENT '政治成績',
    PRIMARY KEY(id)
)ENGINE = INNODB DEFAULT CHARSET = utf8;

插入數據:

INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('001',90,92,80,0);
INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('002',88,90,75.5,0);
INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('003',70,85,90,82);

查詢數據表中的內容(即轉換前的結果)

SELECT * FROM tb_score1

轉換後:

本質是將userid的每一個科目分數分散成一條記錄顯示出來。

直接上SQL:

SELECT userid,'語文' AS course,cn_score AS score FROM tb_score1
UNION ALL
SELECT userid,'數學' AS course,math_score AS score FROM tb_score1
UNION ALL
SELECT userid,'英語' AS course,en_score AS score FROM tb_score1
UNION ALL
SELECT userid,'政治' AS course,po_score AS score FROM tb_score1
ORDER BY userid

這裏將每一個userid對應的多個科目的成績查出來,經過UNION ALL將結果集加起來,達到上圖的效果。

附:UNION與UNION ALL的區別(摘):

1.對重複結果的處理:UNION會去掉重複記錄,UNION ALL不會;

2.對排序的處理:UNION會排序,UNION ALL只是簡單地將兩個結果集合並;

3.效率方面的區別:由於UNION 會作去重和排序處理,所以效率比UNION ALL慢不少;

相關文章
相關標籤/搜索