mysql行列互相转换
列转行:
mysql> select * from test; +------+----------+-------+
| id | subject | score |
+------+----------+-------+
| 1 | chinese | 98 |
| 2 | math | 95 |
| 2 | politics | 87 |
| 5 | chinese | 97 |
| 5 | math | 100 |
| 5 | politics | 92 |
+------+----------+-------+
6 rows in set (0.00 sec) mysql> select (case id when 1 then 'first' when 2 then 'second' when 5 then 'fifth' end) grade,sum(case subject when 'chinese' then score else 0 end) chinese,sum(case subject when 'math' then score else 0 end) math,sum(case subject when 'politics' then score else 0 end) politics from test group by id;
+--------+---------+------+----------+
| grade | chinese | math | politics |
+--------+---------+------+----------+
| first | 98 | 0 | 0 |
| second | 0 | 95 | 87 |
| fifth | 97 | 100 | 92 |
+--------+---------+------+----------+
3 rows in set (0.00 sec)
行转列:
复制上面的结果到一个新表test2
mysql> create table test2 as select (case id when 1 then 'first' when 2 then 'second' when 5 then 'fifth' end) grade,sum(case subject when 'chinese' then score else 0 end) chinese,sum(case subject when 'math' then score else 0 end) math,sum(case subject when 'politics' then score else 0 end) politics from test group by id;
Query OK, 3 rows affected (0.12 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from test2;
+--------+---------+------+----------+
| grade | chinese | math | politics |
+--------+---------+------+----------+
| first | 98 | 0 | 0 |
| second | 0 | 95 | 87 |
| fifth | 97 | 100 | 92 |
+--------+---------+------+----------+
3 rows in set (0.00 sec)
mysql> select * from (select (case grade when 'first' then 1 when 'second' then 2 when 'fifth' then 5 end) id,'chinese' subject,chinese as score from test2 union all select (case grade when 'first' then 1 when 'second' then 2 when 'fifth' then 5 end) id,'math' subject,math as score from test2 union all select (case grade when 'first' then 1 when 'second' then 2 when 'fifth' then 5 end) id,'politics' subject,politics as score from test2 order by id) a where a.score<>0;
+------+----------+-------+
| id | subject | score |
+------+----------+-------+
| 1 | chinese | 98 |
| 2 | math | 95 |
| 2 | politics | 87 |
| 5 | math | 100 |
| 5 | politics | 92 |
| 5 | chinese | 97 |
+------+----------+-------+
6 rows in set (0.00 sec) mysql> select * from test;
+------+----------+-------+
| id | subject | score |
+------+----------+-------+
| 1 | chinese | 98 |
| 2 | math | 95 |
| 2 | politics | 87 |
| 5 | chinese | 97 |
| 5 | math | 100 |
| 5 | politics | 92 |
+------+----------+-------+
6 rows in set (0.00 sec)
mysql行列互相转换的更多相关文章
- mysql 行列动态转换(列联表,交叉表)
mysql 行列动态转换(列联表,交叉表) (1)动态,适用于列不确定情况 create table table_name( id int primary key, col1 char(2), col ...
- 【转载】mysql行列转换方法总结
[转载]mysql行列转换方法总结 [MySQL] 行列转换变化各种方法实现总结(行变列报表统计.列变行数据记录统计等) Mysql 列转行统计查询 .行转列统计查询 在某些数据库中有交叉表,但在My ...
- mysql 日期 时间戳 转换
/***************************************************************************************** * mysql 日 ...
- mysql 类型自动化转换问题
mysql 类型自动化转换问题 背景 有个业务需求,使用到find_in_set函数,简单贴下,如下: SELECT FIND_IN_SET('b','a,b,c,d'); //返回值为2,即第2个 ...
- mysql查询时间戳转换
mysql查询时间戳转换 SELECT FROM_UNIXTIME(create_time) FROM tablename; 更新时间为七天以后 UPDATE t_rebate_trade_item ...
- MySQL隐式转换的坑
MySQL以以下规则描述比较操作如何进行转换: 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做 ...
- 【学亮IT手记】MySql行列转换案例
create table score( name ), math int, english int ); ,); ,); ,); ,); SHOW tables; SELECT * from scor ...
- Mysql 行列转换
一.第一种 原数据表 转换后 DROP TABLE IF EXISTS tempdynamic; CREATE TEMPORARY TABLE tempdynamic ( SELECT p.fsPay ...
- MySQL行列转换
分类: Mysql/postgreSQL 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义.http://topic.csdn.net/u ...
随机推荐
- Http服务器实现文件上传与下载(四)
一.引言 欢迎大家来到和我一起编写Http服务器实现文件的上传和下载,现在我稍微回顾一下之前我说的,第一.二章说明说明了整体的HTTP走向,第三章实现底层的网络编程.接着这一章我想给大家讲的是请求获取 ...
- python之list去重
问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变. 直观方法 最简单的思路就是: 复制代码代码如下: ids = [1,2,3,3,4,2,3,4,5,6,1]news_ids = ...
- 认识tornado(三)
实际上handler有很多讲究,在Application类的注释中,就讲了不少. 1. 首先,(regexp,tornado.web.RequestHandler)中的第一个参数不是普通的字符串,而是 ...
- Sql Server 数据库用Transact-SQL语句创建链接服务器
1.在查询编辑器中,输入以下 Transact-SQL 命令以便链接到名为 SRVR002\ACCTG 的 SQL Server 实例: USE [master] GO EXEC master.dbo ...
- ASP.NET MVC 数据库依赖缓存
ASP.NET MVC 数据库依赖缓存 问题背景 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法 ...
- 【BZOJ4552】[Tjoi2016&Heoi2016]排序 二分+线段树
[BZOJ4552][Tjoi2016&Heoi2016]排序 Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ...
- oracle的order by decode根据文字自定义排序的例子
oracle的order by decode根据文字自定义排序的例子: order by decode(t.title, '当前生效预警', 1, '今日即将生效', 2, '明日预计生效', 3, ...
- 第一个Gradle入门程序
参考:http://www.importnew.com/15881.html 准备工作 1.gradle编译环境 下载gradle编译包(http://www.gradle.org/downloads ...
- Centos6.5下Samba服务器的安装和配置
1.安装samba服务 # yum install samba samba-client samba-swat 2.安装包说明 samba-3.6.23-43.el6_9.x86_64----> ...
- HTTP协议包分析(小马上传大马)
最近工作内容是分析防火墙日志,看日志是正确,本地实验小马上传大马 抓取http包如下.可以在分析过程中进行借鉴. 该http请求的行为是通过小马,在小马的当前目录创建一个dama.php的文件,文件 ...