mysql 行转列与列传行
孙中明 人气:0一、行转列— case+group by
mysql> CREATE TABLE `TEST_TB_GRADE` ( -> `ID` int(10) NOT NULL AUTO_INCREMENT, -> `USER_NAME` varchar(20) DEFAULT NULL, -> `COURSE` varchar(20) DEFAULT NULL, -> `SCORE` float DEFAULT '0', -> PRIMARY KEY (`ID`) -> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
插入数据:
mysql> insert into TEST_TB_GRADE(USER_NAME, COURSE, SCORE) values -> ("张三", "数学", 34), -> ("张三", "语文", 58), -> ("张三", "英语", 58), -> ("李四", "数学", 45), -> ("李四", "语文", 87), -> ("李四", "英语", 45), -> ("王五", "数学", 76), -> ("王五", "语文", 34), -> ("王五", "英语", 89);
查询表:
mysql> select * from test_tb_grade; +----+-----------+--------+-------+ | ID | USER_NAME | COURSE | SCORE | +----+-----------+--------+-------+ | 1 | 张三 | 数学 | 34 | | 2 | 张三 | 语文 | 58 | | 3 | 张三 | 英语 | 58 | | 4 | 李四 | 数学 | 45 | | 5 | 李四 | 语文 | 87 | | 6 | 李四 | 英语 | 45 | | 7 | 王五 | 数学 | 76 | | 8 | 王五 | 语文 | 34 | | 9 | 王五 | 英语 | 89 | +----+-----------+--------+-------+
不用聚集函数和group by语句:
mysql> SELECT user_name , -> (CASE course WHEN '数学' THEN score ELSE 0 END ) 数学, -> (CASE course WHEN '语文' THEN score ELSE 0 END ) 语文, -> (CASE course WHEN '英语' THEN score ELSE 0 END ) 英语 -> FROM test_tb_grade; +-----------+--------+--------+--------+ | user_name | 数学 | 语文 | 英语 | +-----------+--------+--------+--------+ | 张三 | 34 | 0 | 0 | | 张三 | 0 | 58 | 0 | | 张三 | 0 | 0 | 58 | | 李四 | 45 | 0 | 0 | | 李四 | 0 | 87 | 0 | | 李四 | 0 | 0 | 45 | | 王五 | 76 | 0 | 0 | | 王五 | 0 | 34 | 0 | | 王五 | 0 | 0 | 89 | +-----------+--------+--------+--------+
用group by语句:
mysql> SELECT user_name , -> (CASE course WHEN '数学' THEN score ELSE 0 END ) 数学, -> (CASE course WHEN '语文' THEN score ELSE 0 END ) 语文, -> (CASE course WHEN '英语' THEN score ELSE 0 END ) 英语 -> FROM test_tb_grade -> group by user_name; +-----------+--------+--------+--------+ | user_name | 数学 | 语文 | 英语 | +-----------+--------+--------+--------+ | 张三 | 34 | 0 | 0 | | 李四 | 45 | 0 | 0 | | 王五 | 76 | 0 | 0 | +-----------+--------+--------+--------+
用group by语句和聚集函数实现行转列:
mysql> SELECT user_name , -> MAX(CASE course WHEN '数学' THEN score ELSE 0 END ) 数学, -> MAX(CASE course WHEN '语文' THEN score ELSE 0 END ) 语文, -> MAX(CASE course WHEN '英语' THEN score ELSE 0 END ) 英语 -> FROM test_tb_grade -> group by user_name; +-----------+--------+--------+--------+ | user_name | 数学 | 语文 | 英语 | +-----------+--------+--------+--------+ | 张三 | 34 | 58 | 58 | | 李四 | 45 | 87 | 45 | | 王五 | 76 | 34 | 89 | +-----------+--------+--------+--------+
二、列转行——union
CREATE TABLE `TEST_TB_GRADE2` ( `ID` int(10) NOT NULL AUTO_INCREMENT, `USER_NAME` varchar(20) DEFAULT NULL, `CN_SCORE` float DEFAULT NULL, `MATH_SCORE` float DEFAULT NULL, `EN_SCORE` float DEFAULT '0', PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
插入数据:
insert into TEST_TB_GRADE2(USER_NAME, CN_SCORE, MATH_SCORE, EN_SCORE) values ("张三", 34, 58, 58), ("李四", 45, 87, 45), ("王五", 76, 34, 89);
查询:
mysql> select * from test_tb_grade2; +----+-----------+----------+------------+----------+ | ID | USER_NAME | CN_SCORE | MATH_SCORE | EN_SCORE | +----+-----------+----------+------------+----------+ | 1 | 张三 | 34 | 58 | 58 | | 2 | 李四 | 45 | 87 | 45 | | 3 | 王五 | 76 | 34 | 89 | +----+-----------+----------+------------+----------+
不求并集:
mysql> select user_name, '语文' COURSE , CN_SCORE as SCORE from test_tb_grade2; +-----------+--------+-------+ | user_name | COURSE | SCORE | +-----------+--------+-------+ | 张三 | 语文 | 34 | | 李四 | 语文 | 45 | | 王五 | 语文 | 76 | +-----------+--------+-------+
求并集:
mysql> select user_name, '语文' COURSE , CN_SCORE as SCORE from test_tb_grade2 -> union select user_name, '数学' COURSE, MATH_SCORE as SCORE from test_tb_grade2 -> union select user_name, '英语' COURSE, EN_SCORE as SCORE from test_tb_grade2; +-----------+--------+-------+ | user_name | COURSE | SCORE | +-----------+--------+-------+ | 张三 | 语文 | 34 | | 李四 | 语文 | 45 | | 王五 | 语文 | 76 | | 张三 | 数学 | 58 | | 李四 | 数学 | 87 | | 王五 | 数学 | 34 | | 张三 | 英语 | 58 | | 李四 | 英语 | 45 | | 王五 | 英语 | 89 | +-----------+--------+-------+
order by语句:
mysql> select user_name, '语文' COURSE , CN_SCORE as SCORE from test_tb_grade2 -> union select user_name, '数学' COURSE, MATH_SCORE as SCORE from test_tb_grade2 -> union select user_name, '英语' COURSE, EN_SCORE as SCORE from test_tb_grade2 -> order by user_name,COURSE; +-----------+--------+-------+ | user_name | COURSE | SCORE | +-----------+--------+-------+ | 张三 | 数学 | 58 | | 张三 | 英语 | 58 | | 张三 | 语文 | 34 | | 李四 | 数学 | 87 | | 李四 | 英语 | 45 | | 李四 | 语文 | 45 | | 王五 | 数学 | 34 | | 王五 | 英语 | 89 | | 王五 | 语文 | 76 | +-----------+--------+-------+
加载全部内容