MySql 動態行轉列整理
在開發過程中,我們或許經常碰到這樣的需求,即將某些sql查詢資料實現動態行轉列。
舉例來說:一個學生參加過多次考試,如果想知道該學生最近幾次考試語文的成績,如下圖:
對於使用者來說,我們希望看到的如下圖(即資料動態實現行轉列):
下面看資料表結構:
CREATE TABLE `e_exam` ( `exam_id` int(11) NOT NULL DEFAULT '0' COMMENT '考試名稱', `exam_name` varchar(50) DEFAULT NULL, `schoolbaseinfo_id` varchar(36) DEFAULT NULL COMMENT '歸屬學校', `gradeinfo_id` varchar(36) DEFAULT NULL COMMENT '年級', `exam_date` date DEFAULT NULL, `status` varchar(10) DEFAULT NULL COMMENT '狀態:new, open, close', `last_update_by` varchar(36) DEFAULT NULL, `last_update_time` datetime DEFAULT NULL, PRIMARY KEY (`exam_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='考試資訊表'
CREATE TABLE `e_exam_schedule` ( `exam_schedule_id` int(11) NOT NULL DEFAULT '0', `exam_id` int(11) DEFAULT NULL, `subject_id` int(11) DEFAULT NULL COMMENT '學科', `subject_name` varchar(20) DEFAULT NULL, `full_marks` int(11) DEFAULT NULL COMMENT '卷面值(滿分成績)', `begin_time` datetime DEFAULT NULL COMMENT '開始時間', `end_time` datetime DEFAULT NULL COMMENT '結束時間', `last_update_by` varchar(36) DEFAULT NULL, `last_update_time` datetime DEFAULT NULL, `zuowen` tinyint(4) DEFAULT '0' COMMENT '是否含作文 0:不含 1: 含', `duration` int(11) DEFAULT NULL COMMENT '考試持續時間 分鐘', PRIMARY KEY (`exam_schedule_id`), KEY `exam_id` (`exam_id`), CONSTRAINT `e_exam_schedule_ibfk_1` FOREIGN KEY (`exam_id`) REFERENCES `e_exam` (`exam_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `e_score` ( `score_id` int(11) NOT NULL DEFAULT '0', `exam_schedule_id` int(11) DEFAULT NULL COMMENT '考試計劃(學科)', `studentbaseinfo_id` varchar(36) DEFAULT NULL COMMENT '學生', `score` double DEFAULT NULL COMMENT '分數', `remark` varchar(20) DEFAULT NULL COMMENT '備註', `last_update_by` varchar(36) DEFAULT NULL, `last_update_time` datetime DEFAULT NULL, PRIMARY KEY (`score_id`), KEY `exam_schedule_id` (`exam_schedule_id`), KEY `studentbaseinfo_id` (`studentbaseinfo_id`), CONSTRAINT `e_score_ibfk_1` FOREIGN KEY (`exam_schedule_id`) REFERENCES `e_exam_schedule` (`exam_schedule_id`), CONSTRAINT `e_score_ibfk_2` FOREIGN KEY (`studentbaseinfo_id`) REFERENCES `t_student` (`studentbaseinfo_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
下面是儲存過程的實現:
DELIMITER $$
USE `homework_test`$$
DROP PROCEDURE IF EXISTS `score_statistical`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `score_statistical`(IN grade_id VARCHAR(50), IN subject_id INT, IN class_id VARCHAR(50), IN date_range INT)
BEGIN
DECLARE $rowcnt INT; /* row count */
DECLARE $i INT; /* temp variable */
SET @EE=''; /* the final column,such as: SUM(IF(exam_id=330573,score,0)) AS a330573,SUM(IF(exam_id=330574,score,0)) AS a330574, */
SET $i=1;
SET @daterange= IF(date_range=1, ' ', ' t1.exam_date >= DATE_SUB(NOW(), INTERVAL 3 MONTH) AND ' );
SET @table1=CONCAT('SELECT t4.studentbaseinfo_id AS studentbaseinfo_id,t4.xuhao,t4.real_name as real_name,IFNULL(t1.exam_id,\'average\') AS exam_id,AVG(t3.score) AS score FROM
e_exam t1,e_exam_schedule t2,e_score t3,t_student t4 WHERE t2.exam_id=t1.exam_id AND
t2.exam_schedule_id = t3.exam_schedule_id AND t3.studentbaseinfo_id=t4.studentbaseinfo_id AND t1.status!=\'new\' AND',
@daterange,
't1.gradeinfo_id=\'', grade_id, '\' AND t2.subject_id=',subject_id, ' AND t4.classinfo_id=\'',class_id,
'\' GROUP BY t4.studentbaseinfo_id, t1.exam_id WITH ROLLUP HAVING t4.studentbaseinfo_id IS NOT NULL'); /*the final sql */
/* count the row number */
SELECT COUNT(DISTINCT(t.exam_id)) INTO $rowcnt FROM e_exam t,e_exam_schedule k WHERE t.exam_id=k.exam_id AND IF(date_range=1, 1=1, t.exam_date >= DATE_SUB(NOW(), INTERVAL 3 MONTH))
AND t.gradeinfo_id=grade_id AND t.status!='new' AND k.subject_id=subject_id;
/* make up the final column */
WHILE $i <= $rowcnt DO
SET @i:=0;
SELECT col FROM (SELECT (@i:[email protected]+1) AS iden, CONCAT(@EE,'SUM(IF(exam_id=', exam_id, ',score,0)) AS a',exam_id,',') AS col
FROM (SELECT DISTINCT(m.exam_id) FROM e_exam m,e_exam_schedule n WHERE m.exam_id=n.exam_id AND
IF(date_range=1, 1=1, m.exam_date>=DATE_SUB(NOW(), INTERVAL 3 MONTH)) AND m.gradeinfo_id=grade_id AND m.status!='new'
AND n.subject_id=subject_id) AS tb)
AS tb1 WHERE iden=$i INTO @EE;
SET $i:=$i+1;
END WHILE;
/*
SELECT @EE:=CONCAT(@EE,'SUM(IF(exam_id=',exam_id,',score,0)) AS a',exam_id,',') FROM (
SELECT DISTINCT(exam_id) FROM e_exam WHERE gradeinfo_id='F710895E-E648-483E-8C0B-76EAB79CCD17') A;
*/
SET @QQ=CONCAT('SELECT xuhao, real_name,', @EE, 'SUM(IF(exam_id=\'average\',score, 0)) AS average FROM (', @table1,') tx GROUP BY tx.studentbaseinfo_id HAVING tx.studentbaseinfo_id IS NOT NULL');
PREPARE stmt2 FROM @QQ;
EXECUTE stmt2;
END$$
DELIMITER ;
呼叫:
CALL `score_statistical`('F710895E-E648-483E-8C0B-76EAB79CCD17', 1, '7D2F41ED-2148-418E-85C9-5AFC43E21C37', 1)
下面是實現過程中參考的文章:
文章來源一(感謝ACMAIN_CHM的徵集帖子):
資料指令碼:
CREATE TABLE `tx` (
`id` int(11) NOT NULL,
`c1` char(2) DEFAULT NULL,
`c2` char(2) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
insert into `tx`(`id`,`c1`,`c2`,`c3`) 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);
mysql> select * from tx;
+----+------+------+------+
| id | c1 | c2 | c3 |
+----+------+------+------+
| 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 |
+----+------+------+------+
20 rows in set (0.00 sec)
期望結果
+------+-----+-----+-----+-----+------+
|C1 |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 |
+------+-----+-----+-----+-----+------+
1. 利用SUM(IF()) 生成列 + WITH ROLLUP 生成彙總行,並利用 IFNULL將彙總行標題顯示為 Total
mysql> SELECT
-> IFNULL(c1,'total') AS total,
-> SUM(IF(c2='B1',c3,0)) AS B1,
-> SUM(IF(c2='B2',c3,0)) AS B2,
-> SUM(IF(c2='B3',c3,0)) AS B3,
-> SUM(IF(c2='B4',c3,0)) AS B4,
-> SUM(IF(c2='total',c3,0)) AS total
-> FROM (
-> SELECT c1,IFNULL(c2,'total') AS c2,SUM(c3) AS c3
-> FROM tx
-> GROUP BY c1,c2
-> WITH ROLLUP
-> HAVING c1 IS NOT NULL
-> ) AS A
-> GROUP BY c1
-> WITH ROLLUP;
+-------+------+------+------+------+-------+
| total | 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 |
+-------+------+------+------+------+-------+
5 rows in set, 1 warning (0.00 sec)
2. 利用SUM(IF()) 生成列 + UNION 生成彙總行,並利用 IFNULL將彙總行標題顯示為 Total
mysql> select c1,
-> sum(if(c2='B1',C3,0)) AS B1,
-> sum(if(c2='B2',C3,0)) AS B2,
-> sum(if(c2='B3',C3,0)) AS B3,
-> sum(if(c2='B4',C3,0)) AS B4,SUM(C3) AS TOTAL
-> from tx
-> group by C1
-> UNION
-> SELECT 'TOTAL',sum(if(c2='B1',C3,0)) AS B1,
-> sum(if(c2='B2',C3,0)) AS B2,
-> sum(if(c2='B3',C3,0)) AS B3,
-> sum(if(c2='B4',C3,0)) AS B4,SUM(C3) FROM TX
-> ;
+-------+------+------+------+------+-------+
| c1 | 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 |
+-------+------+------+------+------+-------+
5 rows in set (0.00 sec)
mysql>
3. 利用SUM(IF()) 生成列,直接生成結果不再利用子查詢
mysql> select ifnull(c1,'total'),
-> sum(if(c2='B1',C3,0)) AS B1,
-> sum(if(c2='B2',C3,0)) AS B2,
-> sum(if(c2='B3',C3,0)) AS B3,
-> sum(if(c2='B4',C3,0)) AS B4,SUM(C3) AS TOTAL
-> from tx
-> group by C1 with rollup ;
+--------------------+------+------+------+------+-------+
| ifnull(c1,'total') | 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 |
+--------------------+------+------+------+------+-------+
5 rows in set (0.00 sec)
mysql>
4. 動態,適用於列不確定情況,
mysql> SET @EE='';
mysql> SELECT @EE:=CONCAT(@EE,'SUM(IF(C2=/'',C2,'/'',',C3,0)) AS ',C2,',') FROM (SELECT DISTINCT C2 FROM TX) A;
mysql> SET @QQ=CONCAT('SELECT ifnull(c1,/'total/'),',LEFT(@EE,LENGTH(@EE)-1),' ,SUM(C3) AS TOTAL FROM TX GROUP BY C1 WITH ROLLUP');
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE stmt2 FROM @QQ;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE stmt2;
+--------------------+------+------+------+------+-------+
| ifnull(c1,'total') | 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 |
+--------------------+------+------+------+------+-------+
5 rows in set (0.00 sec)
資料庫中也可以用 CASE WHEN / DECODE 代替 IF
文章來源二(感謝hrb2008):
mysql 中設定為支援中文vi etc/my.cnf
[mysqld]
#adddefault-character-set=gb2312
建表
droptable table_name
createtable table_name( mon varchar(10),per varchar(10),value int)
插入資料
insertinto table_name
select'一月','張三',10UNIONallselect'二月','李四',30UNIONallselect'三月','王五',50UNIONallselect'四月','陳六',60UNIONallselect'五月','劉七',78UNIONallselect'一月','陳八',90
儲存過程aa DELIMITER $$
DROP PROCEDURE IF EXISTS `radius`.`aa`$$
CREATE DEFINER=`root`@`%` PROCEDURE `aa`()
BEGIN
#tangwf 2007-11-23
declare $stm varchar(500);
declare $rowcnt int;
declare $mycnt int;
declare $i int;
set $mycnt = 0;
set $i = 1;
set $stm='select per';
select count(distinct mon) into $rowcnt from table_name;
WHILE $i<=$rowcnt DO
set @i :=0;
select col from (select (@i := @i +1 ) as iden,CONCAT($stm,',SUM(case mon when ''',mon,''' then value else 0 end) as ''',mon,'''') as col from (select distinct mon from table_name) as tb) as tb1 where iden=$i into $stm;
Set $i:=$i+1;
END WHILE;
Set @stm=concat($stm,' from table_name group by per;');
prepare s from @stm;
execute s;
deallocate prepare s;
END$$
執行儲存過程
call aa()
結果
per 一月 二月 三月 四月 五月
李四 030000
劉七 000078
王五 005000
張三 100000
陳六 000600
陳八 900000
文章來源三(感謝yueliangdao0608):
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_row_column_wrap`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_row_column_wrap`(IN $schema_name varchar(64),
IN $table_name varchar(64))
BEGIN
declare cnt int(11);
declare $table_rows int(11);
declare i int(11);
declare j int(11);
declare s int(11);
declare str varchar(255);
-- Get the column number of the table
select count(1) from information_schema.columns where table_schema=$schema_name andtable_name=$table_name into cnt;
-- Get the row number of the table
select table_rows from information_schema.tables where table_schema = $schema_name andtable_name=$table_name into $table_rows;
-- Check whether the table exists or not
drop table if exists test.temp;
create table if not exists test.temp (`1` varchar(255) not null);
-- loop1 start
set i = 0;
loop1:loop
if i = $table_rows-1 then
leave loop1;
end if;
set @stmt1 = concat('alter
table test.temp add `',i+2,'` varchar(255) not null');
prepare s1 from @stmt1;
execute s1;
deallocate prepare s1;
set @stmt1 = '';
set i = i + 1;
end loop loop1;
-- loop1 end;
set s = 0;
-- loop2 start
loop2:loop
-- leave loop2
if s=cnt then
leave loop2;
end if;
set @stmt2 = concat('select
column_name from information_schema.columns where table_schema="',$schema_name,
'" and table_name="',$table_name,'"
limit ',s,',1 into @temp;');
prepare s2 from @stmt2;
execute s2;
deallocate prepare s2;
set @stmt2 = '';
set j=0;
set str = ' select ';
-- Loop3 start
loop3:loop
if j = $table_rows then
leave loop3;
end if;
set @stmt3 = concat('select
',@temp,' from ',$schema_name,'.',$table_name,'
limit ',j,',1 into @temp2;');
prepare s3 from @stmt3;
execute s3;
set str = concat(str,'"',@temp2,'"',',');
deallocate prepare s3;
set @stmt3 = '';
set j = j+1;
end loop loop3;
set str = left(str,length(str)-1);
-- insert new data into table
set @stmt4 = concat('insert
into test.temp',str,';');
prepare s4 from @stmt4;
execute s4;
deallocate prepare s4;
set @stmt4 = '';
set s=s+1;
end loop loop2;
END$$
DELIMITER ;
以下是測試結果:
======
select * from a;
select * from b;
select * from salary;
call sp_row_column_wrap('test','a');
select * from test.temp;
call sp_row_column_wrap('test','b');
select * from test.temp;
call sp_row_column_wrap('test','salary');
select * from test.temp;
query result(2 records)
aid | title |
1 | 111 |
2 | 222 |
query result(3 records)
bid | aid | image | time |
1 | 2 | 1.gif | 2007-08-08 |
2 | 2 | 2.gif | 2007-08-09 |
3 | 2 | 3.gif | 2007-08-08 |
query result(7 records)
id | cost | des | Autoid |
1 | 10 | aaaa | 1 |
1 | 15 | bbbb | 2 |
1 | 20 | cccc | 3 |
2 | 80 | aaaa | 4 |
2 | 100 | bbbb | 5 |
2 | 60 | dddd | 6 |
3 | 500 | dddd | 7 |
query result(2 records)
1 | 2 |
1 | 2 |
111 | 222 |
query result(4 records)
1 | 2 | 3 |
1 | 2 | 3 |
2 | 2 | 2 |
1.gif | 2.gif | 3.gif |
2007-08-08 | 2007-08-09 | 2007-08-08 |
query result(4 records)
1 | 2 | 3 | 4 | 5 | 6 | 7 |
1 | 1 | 1 | 2 | 2 | 2 | 3 |
10 | 15 | 20 | 80 | 100 | 60 | 500 |
aaaa | bbbb | cccc | aaaa | bbbb | dddd | dddd |
1 | 2 | 3 | 4 | 5 | 6 | 7 |
其他參考url:
http://stackoverflow.com/questions/17964078/mysql-query-to-dynamically-convert-rows-to-columns-on-the-basis-of-two-columns
http://dba.stackexchange.com/questions/47902/how-to-transpose-convert-rows-as-columns-in-mysql
http://hancang2010.blog.163.com/blog/static/1824602612011119101416357/ mysql prepare語句使用
http://blog.csdn.net/meirenxing/article/details/6590968 mysql 儲存過程 要點
http://blog.csdn.net/meirenxing/article/details/6585700 mysql的prepare應用
mysql的left,right,substr,instr擷取字串,擷取小數點float
http://hi.baidu.com/bcpxqz/item/c68d8435b5500c20b2c0c5b3
使用 GROUP BY WITH ROLLUP 改善統計效能
http://blog.csdn.net/id19870510/article/details/6254358
MySQL中的WITH ROLLUP
http://blog.csdn.net/lxz3000/article/details/6174678
關於Mysql group by和with rollup的用法小記
http://www.sphinxsearch.org/archives/100
mysql行轉列
http://blog.163.com/weizi_mm/blog/static/31554420122544629224/
http://bbs.csdn.net/topics/320204897
http://bbs.csdn.net/topics/310045927
http://blog.csdn.net/zhoushengchao/article/details/7321688
http://hi.baidu.com/fbuacikdutemp/item/ed4f960eccd47ad71ef04614
http://pmandy-163-com.iteye.com/blog/789326
相關推薦
MySql 動態行轉列整理
在開發過程中,我們或許經常碰到這樣的需求,即將某些sql查詢資料實現動態行轉列。 舉例來說:一個學生參加過多次考試,如果想知道該學生最近幾次考試語文的成績,如下圖: 對於使用者來說,我們希望看到的如下圖(即資料動態實現行轉列): 下面看資料表結構: CREAT
mysql動態行轉列
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 DROP TABLE IF EXISTS
mysql 動態行轉列
前言: mysql的行轉列並沒有mssql中的pivot 測試資料: DROP TABLE IF EXISTS `mytest`; CREATE TABLE `mytest` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cl
MySQL儲存過程的動態行轉列
轉載:http://segmentfault.com/a/1190000004314724?ref=myread MySQL儲存過程中使用動態行轉列 最近做專案關於資料報表處理,然而資料庫儲存格式和報表展現形式不同,需要進行一下行轉列的操作,在做上一個專案的時候也看
oracle 存儲過程-動態行轉列,解決。
pla gin sel rom con left join from blog creat 包頭 create or replace package pro_test as TYPE out_cursor IS REF CURSOR; procedure A
mybatis 動態行轉列
SELECT user_name , MAX(CASE course WHEN '數學' THEN score END ) 數學, MAX(CASE course WHEN '語文' THEN score END ) 語文, MAX(CASE course WHEN '英語'
mySQL的行轉列
因為MYSQL裡邊沒有 PIVOT 現記錄: 原表格: mysql語句: SELECT MAX(CASE WHEN corol='紅' THEN NUM else
SQL Server 動態行轉列(引數化表名、分組列、行轉列欄位、欄位值)
一.本文所涉及的內容(Contents) 二.背景(Contexts) 其實行轉列並不是一個什麼新鮮的話題了,甚至已經被大家說到爛了,網上的很多例子多多少少都有些問題,所以我希望能讓大家快速的看到執行的效果,所以在動態列的基礎上再把表、分組欄位、行轉列欄位、值這四個行轉列固定需要的值變成真正意義的
Mysql-sql行轉列
原始資料如下圖所示:(商品的銷售明細)date=業務日期;Item=商品名稱;saleqty=銷售數量 -- 建立測試資料(表)create table test (Date varchar(10), item char(10),saleqty int);insert test values('2010
在論壇中出現的比較難的sql問題:39(動態行轉列 動態日期列問題)
最近,在論壇中,遇到了不少比較難的sql問題,雖然自己都能解決,但發現過幾天后,就記不起來了,也忘記解決的方法了。 所以,覺得有必要記錄下來,這樣以後再次碰到這類問題,也能從中獲取解答的思路。 求一SQL語句。 create table #tab ( col1
MySQL 實現行轉列SQL
概述 好久沒寫SQL語句,今天看到問答中的一個問題,拿來研究一下。 情景簡介 學校裡面記錄成績,每個人的選課不一樣,而且以後會新增課程,所以不需要把所有課程當作列。資料表裡面數據如下圖,使用姓名+課程作為聯合主鍵(有些需求可能不需要聯合主鍵)。本文以MySQL為基
資料庫動態行轉列
--行列互轉 /*********************************************************************************************************************************
MySQL的行轉列、列轉行、連線字串 concat、concat_ws、group_concat函式用法
1.concat函式 使用方法: CONCAT(str1,str2,…) 返回結果為連線引數產生的字串。如有任何一個引數為NULL ,則返回值為 NULL。 注意: 如果所有引數均為非二進位制字串,則結果為非二進位制字串。 如果自變
mysql簡單行轉列
DROP TABLE IF EXISTS `tb`; CREATE TABLE `tb` ( `id` varchar(12) CHARACTER SET utf8 COLLATE utf8_gene
mysql 行轉列
行轉列最近遇到一需求原始數據如下:mysql> select id,sdkname,sid,date,total_count from u1ge_query_log;+------+----------------+------+------------+-------------+| id | s
mysql行轉列轉換
pan where mys 作用 統計 detail null 這就是 英語 http://blog.csdn.net/sinat_27406925/article/details/77507478 mysql 行列轉換 ,在項目中應用的極其頻繁,尤其是一些金融項目裏的報表
MySQL行轉列與列轉行
展示 course order by rem core null innodb tail mysql 行轉列 例如:把圖1轉換成圖2結果展示 圖1 圖2 CREATE TABLE `TEST_TB_GRADE` ( `ID` int(10) NOT NU
MySQL行轉列經典案例
準備資料表 /* Navicat MySQL Data Transfer Source Server : MyDB Source Server Version : 50720 Source Host : localhost:3306 S
SQL行轉列的動態構造方法
SQL行轉列的動態構造方法 /*假設有張學生成績表(tb)如下: 姓名 課程 分數 張三 語文 74 張三 數學 83 張三 物理 93 李四 語文 74 李四 數學 84 李四 物理 94 想變成(得到如下結果): 姓名 語文 數學 物理 ---- ---- ---- ---- 李四 74 8
mysql-行轉列
mysql-行轉列 行轉列列傳行更具體例子 1、檢視資料 Sql程式碼 SELECT * FROM tabName ; &