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 ...
随机推荐
- 如何用MathType编辑这三个符号
MathType是一款专门的公式编辑器,用来编辑数学物理等公式,很多期刊杂志的排版都会用到它.用MathType编辑公式的时候,完全不用考虑学习和上手的过程,打开就可以编辑出你的公式,所以这个工具对于 ...
- 解决Ajax请求跨域问题
from:https://blog.csdn.net/wang379275614/article/details/53333775 上篇文章提到,由于浏览器的同源策略,使得,AJAX请求只能发给同源的 ...
- 如何使用C#操作WinAPI
Windows API是对Windows操作系统的API函数,在C#中调用Windows API的实质是托管代码对非托管代码的调用. 主要使用的格式就是: using System.Runtime.I ...
- spring 和 struts 整合遇到的问题(学习中)
一大早就报错 org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.eng ...
- jq 选择器基础及拓展
jquery 用的很多,所以jq的选择器就很受欢迎,但是用的过程中有一些小问题,如果不点透就永远不知道. 1:ID选择器:$("#ID"); 得到一个指定对应,并且只能得到一个对象 ...
- delphi -----(去掉窗口最大化,最小化、关闭),主窗口,和子窗口之间的设置
一.去掉窗口最大化,最小化.关闭 borderIcons:biSystemMenu:false borderStyle:bsSizeable 二.主子窗口 主main: //调用子窗体procedur ...
- UIButton+Block
UIButton的一个Category,使用block处理UIControlEvent事件,如常用的TouchUpInside等.代码非原创,也是从网上看到的,用到了实际项目中,目前还没发现什么问题. ...
- python 里面的单下划线与双下划线的区别(私有和保护)
Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx 不能用'from moduleimport *'导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划 ...
- 接口测试工具 — jmeter(数据库操作)
1.导入jdbc jar包 2.配置MySQL连接 3.执行sql语句
- 原!!mysql,几十万条数据中随机抽取1万以内的数据
想了几种方法: 1.将所有符合条件的对象集合都查出来,在代码里做随机. 2.先查出所有符合条件的id,再代码随机需要抽查数量的id,再 到数据库 中 in. 3.利用order by rand() l ...