数据库行转列和列转行小例子
2015-11-30 22:33
323 查看
有时候,我们想从另一个角度看一张表。这时候就会涉及行列的转换。假如有一张成绩表
mysql> select * from scores;
+------+----------+-------+
| name | kemu | score |
+------+----------+-------+
| A | chinese | 90 |
| A | math | 96 |
| A | english | 79 |
| A | computer | 89 |
| B | computer | 93 |
| B | english | 92 |
| C | english | 77 |
| C | chinese | 78 |
+------+----------+-------+
现在我们想看下A同学的语文,数学,英语分数,希望能列在一行里。这时我们就需要把行转成列。
我们使用case when 语句
select name,sum(case kemu when 'chinese' then score end) as chinese,
sum(case kemu when 'math' then score end) as math,
sum(case kemu when 'english' then score end) as english,
sum(case kemu when 'computer' then score end) as computer
from scores group by name;
+------+---------+------+---------+----------+
| name | chinese | math | english | computer |
+------+---------+------+---------+----------+
| A | 90 | 96 | 79 | 89 |
| B | NULL | NULL | 92 | 93 |
| C | 78 | NULL | 77 | NULL |
+------+---------+------+---------+----------+
列转行
假如有一张成绩表为
mysql> select * from scores_name;
+------+---------+------+---------+----------+
| name | chinese | math | english | computer |
+------+---------+------+---------+----------+
| A | 90 | 96 | 79 | 89 |
| B | NULL | NULL | 92 | 93 |
| C | 78 | NULL | 77 | NULL |
+------+---------+------+---------+----------+
3 rows in set (0.00 sec)
我们将它行转列,使用union all
select name,'chinese' as kemu,chinese as score from scores_name union all
select name,'math' as kemu,math as score from scores_name union all
select name,'english' as kemu, english as score from scores_name union all
select name,'computer' as kemu,computer as score from scores_name
+------+----------+-------+
| name | kemu | score |
+------+----------+-------+
| A | chinese | 90 |
| B | chinese | NULL |
| C | chinese | 78 |
| A | math | 96 |
| B | math | NULL |
| C | math | NULL |
| A | english | 79 |
| B | english | 92 |
| C | english | 77 |
| A | computer | 89 |
| B | computer | 93 |
| C | computer | NULL |
+------+----------+-------+
12 rows in set (0.00 sec)
mysql> select * from scores;
+------+----------+-------+
| name | kemu | score |
+------+----------+-------+
| A | chinese | 90 |
| A | math | 96 |
| A | english | 79 |
| A | computer | 89 |
| B | computer | 93 |
| B | english | 92 |
| C | english | 77 |
| C | chinese | 78 |
+------+----------+-------+
现在我们想看下A同学的语文,数学,英语分数,希望能列在一行里。这时我们就需要把行转成列。
我们使用case when 语句
select name,sum(case kemu when 'chinese' then score end) as chinese,
sum(case kemu when 'math' then score end) as math,
sum(case kemu when 'english' then score end) as english,
sum(case kemu when 'computer' then score end) as computer
from scores group by name;
+------+---------+------+---------+----------+
| name | chinese | math | english | computer |
+------+---------+------+---------+----------+
| A | 90 | 96 | 79 | 89 |
| B | NULL | NULL | 92 | 93 |
| C | 78 | NULL | 77 | NULL |
+------+---------+------+---------+----------+
列转行
假如有一张成绩表为
mysql> select * from scores_name;
+------+---------+------+---------+----------+
| name | chinese | math | english | computer |
+------+---------+------+---------+----------+
| A | 90 | 96 | 79 | 89 |
| B | NULL | NULL | 92 | 93 |
| C | 78 | NULL | 77 | NULL |
+------+---------+------+---------+----------+
3 rows in set (0.00 sec)
我们将它行转列,使用union all
select name,'chinese' as kemu,chinese as score from scores_name union all
select name,'math' as kemu,math as score from scores_name union all
select name,'english' as kemu, english as score from scores_name union all
select name,'computer' as kemu,computer as score from scores_name
+------+----------+-------+
| name | kemu | score |
+------+----------+-------+
| A | chinese | 90 |
| B | chinese | NULL |
| C | chinese | 78 |
| A | math | 96 |
| B | math | NULL |
| C | math | NULL |
| A | english | 79 |
| B | english | 92 |
| C | english | 77 |
| A | computer | 89 |
| B | computer | 93 |
| C | computer | NULL |
+------+----------+-------+
12 rows in set (0.00 sec)
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- Android之获取手机上的图片和视频缩略图thumbnails
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 数据库链接字符串查询网站
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法