主頁 > 知識庫 > mysql 行列動態(tài)轉(zhuǎn)換的實現(xiàn)(列聯(lián)表,交叉表)

mysql 行列動態(tài)轉(zhuǎn)換的實現(xiàn)(列聯(lián)表,交叉表)

熱門標(biāo)簽:谷歌地圖標(biāo)注位置圖解 沈陽智能外呼系統(tǒng)供應(yīng)商 桂林云電銷機器人收費 東莞外呼企業(yè)管理系統(tǒng) 如何選擇優(yōu)質(zhì)的外呼系統(tǒng) 地圖簡圖標(biāo)注 南通電銷外呼系統(tǒng)哪家強 清遠(yuǎn)申請400電話 手機外呼系統(tǒng)違法嗎

(1)動態(tài),適用于列不確定情況

create table table_name(
 id int primary key,
 col1 char(2),
 col2 char(2),
 col3 int
);
insert into table_name values
(1 ,'A1','B1',9),
(2 ,'A2','B1',7),
(3 ,'A3','B1',4),
(4 ,'A4','B1',2),
(5 ,'A1','B2',2),
(6 ,'A2','B2',9),
(7 ,'A3','B2',8),
(8 ,'A4','B2',5),
(9 ,'A1','B3',1),
(10 ,'A2','B3',8),
(11 ,'A3','B3',8),
(12 ,'A4','B3',6),
(13 ,'A1','B4',8),
(14 ,'A2','B4',2),
(15 ,'A3','B4',6),
(16 ,'A4','B4',9),
(17 ,'A1','B4',3),
(18 ,'A2','B4',5),
(19 ,'A3','B4',2),
(20 ,'A4','B4',5);
select * from table_name;
+----+------+------+------+
| id | col1 | col2 | col3 |
+----+------+------+------+
| 1 | A1  | B1  |  9 |
| 2 | A2  | B1  |  7 |
| 3 | A3  | B1  |  4 |
| 4 | A4  | B1  |  2 |
| 5 | A1  | B2  |  2 |
| 6 | A2  | B2  |  9 |
| 7 | A3  | B2  |  8 |
| 8 | A4  | B2  |  5 |
| 9 | A1  | B3  |  1 |
| 10 | A2  | B3  |  8 |
| 11 | A3  | B3  |  8 |
| 12 | A4  | B3  |  6 |
| 13 | A1  | B4  |  8 |
| 14 | A2  | B4  |  2 |
| 15 | A3  | B4  |  6 |
| 16 | A4  | B4  |  9 |
| 17 | A1  | B4  |  3 |
| 18 | A2  | B4  |  5 |
| 19 | A3  | B4  |  2 |
| 20 | A4  | B4  |  5 |
+----+------+------+------+
SET @EE='';
SELECT @EE:=CONCAT(@EE,'SUM(IF(col2=\'',col2,''',',col3,0)) AS ',col2,',') FROM (SELECT DISTINCT col2 FROM table_name) A;
SET @QQ=CONCAT('SELECT ifnull(col1,\'total') AS columnA,',LEFT(@EE,LENGTH(@EE)-1),' ,SUM(col3) AS TOTAL FROM table_name GROUP BY col1 WITH ROLLUP');
PREPARE stmt2 FROM @QQ;
EXECUTE stmt2;
+---------+------+------+------+------+-------+
| columnA | B1  | B2  | B3  | B4  | TOTAL |
+---------+------+------+------+------+-------+
| A1   |  9 |  2 |  1 |  11 |  23 |
| A2   |  7 |  9 |  8 |  7 |  31 |
| A3   |  4 |  8 |  8 |  8 |  28 |
| A4   |  2 |  5 |  6 |  14 |  27 |
| total  |  22 |  24 |  23 |  40 |  109 |
+---------+------+------+------+------+-------+

(2)第二個字段確定的情況下使用

SELECT
  IFNULL(col1,'total') AS total,
  SUM(IF(col2='B1',col3,0)) AS B1,
  SUM(IF(col2='B2',col3,0)) AS B2,
  SUM(IF(col2='B3',col3,0)) AS B3,
  SUM(IF(col2='B4',col3,0)) AS B4,
  SUM(IF(col2='total',col3,0)) AS total
 FROM (
  SELECT col1,IFNULL(col2,'total') AS col2,SUM(col3) AS col3
  FROM table_name
  GROUP BY col1,col2
  WITH ROLLUP
  HAVING col1 IS NOT NULL
 ) AS A
 GROUP BY col1
 WITH ROLLUP;

注: WITH ROLLUP 用于列上求和; SUM(IF(col2='total',col3,0)) AS total 用于行上求和。

(3)第二個字段確定的情況下使用

select ifnull(col1,'total') AS col1,
 sum(if(col2='B1',col3,0)) AS B1,
 sum(if(col2='B2',col3,0)) AS B2,
 sum(if(col2='B3',col3,0)) AS B3,
 sum(if(col2='B4',col3,0)) AS B4,SUM(col3) AS TOTAL
 from table_name
 group by col1 with rollup ;

以上這篇mysql 行列動態(tài)轉(zhuǎn)換的實現(xiàn)(列聯(lián)表,交叉表)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 數(shù)據(jù)庫實現(xiàn)行列轉(zhuǎn)換(mysql示例)
  • mysql 行列轉(zhuǎn)換的示例代碼

標(biāo)簽:重慶 常德 天津 臨沂 成都 貴州 湖州 內(nèi)蒙古

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql 行列動態(tài)轉(zhuǎn)換的實現(xiàn)(列聯(lián)表,交叉表)》,本文關(guān)鍵詞  mysql,行列,動態(tài),轉(zhuǎn)換,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql 行列動態(tài)轉(zhuǎn)換的實現(xiàn)(列聯(lián)表,交叉表)》相關(guān)的同類信息!
  • 本頁收集關(guān)于mysql 行列動態(tài)轉(zhuǎn)換的實現(xiàn)(列聯(lián)表,交叉表)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章