動態sql 實現行轉列
1、模擬數據
-- 創建表 CREATE TABLE `hang_to_lie` ( `area_name` varchar(255) DEFAULT NULL, `industry_class` varchar(255) DEFAULT NULL, `num` int(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 插入數據 insert into hang_to_lie values ('南灣街道','建築業',297) ,('南灣街道','房地產業',362) ,('南灣街道','批發和零售業',4546) ,('南灣街道','教育',209) ,('吉華街道','建築業',241) ,('吉華街道','房地產業',292) ,('吉華街道','批發和零售業',3654) ,('吉華街道','教育',173) ,('園山街道','建築業',194) ,('園山街道','房地產業',251) ,('園山街道','批發和零售業',3493) ,('園山街道','教育',134) ,('坂田街道','建築業',917) ,('坂田街道','房地產業',853) ,('坂田街道','批發和零售業',18720) ,('坂田街道','教育',527) ,('坪地街道','建築業',215) ,('坪地街道','房地產業',203) ,('坪地街道','批發和零售業',2523) ,('坪地街道','教育',107) ,('坪地街道','采礦業',1) ,('寶龍街道','建築業',295) ,('寶龍街道','房地產業',334) ,('寶龍街道','批發和零售業',5193) ,('寶龍街道','教育',205) ,('布吉街道','建築業',481) ,('布吉街道','房地產業',685) ,('布吉街道','批發和零售業',8283) ,('布吉街道','教育',486) ,('平湖街道','建築業',454) ,('平湖街道','房地產業',536) ,('平湖街道','批發和零售業',12466) ,('平湖街道','教育',318) ,('橫崗街道','建築業',244) ,('橫崗街道','房地產業',290) ,('橫崗街道','批發和零售業',3695) ,('橫崗街道','教育',242) ,('龍城街道','建築業',1001) ,('龍城街道','房地產業',1037) ,('龍城街道','批發和零售業',9617) ,('龍城街道','教育',703) ,('龍城街道','采礦業',1) ,('龍崗街道','建築業',792) ,('龍崗街道','房地產業',1164) ,('龍崗街道','批發和零售業',8481) ,('龍崗街道','教育',343)
2.動態sql
CREATE DEFINER=`root`@`localhost` PROCEDURE `hang_to_lie`(in_tablename VARCHAR(100)) BEGIN -- -- SET @sql = NULL; -- SELECT -- GROUP_CONCAT(DISTINCT -- CONCAT( -- 'MAX(IF(c.banji = ''', -- c.banji, -- ''', c.num, 0)) AS ''', -- c.banji, '''' -- ) -- ) INTO @sql -- FROM banjirenshu c; -- -- SET @sql = CONCAT('Select sex, ', @sql, -- ' From banjirenshu -- Group by sex'); -- PREPARE stmt FROM @sql; -- EXECUTE stmt; -- DEALLOCATE PREPARE stmt; SET @sql1 = NULL; SET @sql = NULL; set @sql1 = CONCAT('SELECT GROUP_CONCAT( DISTINCT CONCAT(', "'",'MAX(IF(industry_class =', "'''",',', 'industry_class',',', "'''",',', 'num, 0)) AS ', "'''",',', 'industry_class',',', "''''",' ) ) INTO @sql FROM ', in_tablename); PREPARE stmt1 FROM @sql1; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; -- select -- CONCAT('Select sex, ', @sql, -- ' From ' ,in_tablename -- ,' Group by sex'); -- -- SET @sql = CONCAT('Select area_name, ', @sql, ' From ' ,in_tablename ,' Group by area_name'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; END
3.行轉列原理
實現行專列的原理 1.sql 拼接完: SELECT GROUP_CONCAT( DISTINCT CONCAT('MAX(IF(industry_class =''',industry_class,''',num, 0)) AS ''',industry_class,'''' ) ) INTO @sql FROM hang_to_lie 2.sql1執行后 MAX(IF(industry_class ='建築業',num, 0)) AS '建築業',MAX(IF(industry_class ='房地產業',num, 0)) AS '房地產業',MAX(IF(industry_class ='批發和零售業',num, 0)) AS '批發和零售業',MAX(IF(industry_class ='教育',num, 0)) AS '教育',MAX(IF(industry_class ='采礦業',num, 0)) AS '采礦業' 作為變量進入sql 3.sql1拼接后 SELECT area_name, MAX( IF ( industry_class = '建築業', num, 0 ) ) AS '建築業', MAX( IF ( industry_class = '房地產業', num, 0 ) ) AS '房地產業', MAX( IF ( industry_class = '批發和零售業', num, 0 ) ) AS '批發和零售業', MAX( IF ( industry_class = '教育', num, 0 ) ) AS '教育', MAX( IF ( industry_class = '采礦業', num, 0 ) ) AS '采礦業' FROM hang_to_lie GROUP BY area_name 4.執行sql后 實現行轉列