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 ...
随机推荐
- unity 打开文件夹并鼠标选中某文件
System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "explor ...
- 学习tornado
http://old.sebug.net/paper/books/tornado/ http://demo.pythoner.com/itt2zh/index.html http://tornado- ...
- JAVA基础面试(五5)
41.a.hashCode() 有什么用?与 a.equals(b) 有什么关系? hashCode() 方法对应对象整型的 hash 值.它常用于基于 hash 的集合类,如 Hash ...
- boost::bind 详解
使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式.其可以支持函数对象.函数.函数指针.成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数 ...
- 【BZOJ2179】FFT快速傅立叶
[BZOJ2179]FFT快速傅立叶 Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位 ...
- [HackerRank] The Longest Common Subsequence
This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...
- oninput事件(解决onkeyup无法监听到复制黏贴)
change事件需要两个条件触发: a)当前对象属性改变,并且是由键盘或鼠标事件激发的(脚本触发无效) b)当前对象失去焦点(onblur) keypress 能监听键盘事件,但鼠标复制黏贴操作就 ...
- window下搭建python开发环境
搭建一个python开发环境比较简单,所以就稍微记录一下. 1.下载python然后安装 2.配置环境变量 3.在eclipse添加PyDev插件 1.下载python 官网:https://www. ...
- Linux中Oracle的sqlplus下退格和Del键无效的问题解决
利用rlwrap工具解决方法 1.安装rlwrap和readline库 CentOS下可以用EPEL的yum源直接安装,步骤如下: (1)RHEL/CentOS/SL Linux 6.x 下安装 EP ...
- 客户也可以申请它使用的最后一个IP地址。如果该客户所在的网络中此IP仍然可用,服务器就可以准许该申请。
http://baike.baidu.com/item/IP地址 公有地址 公有地址(Public address)由Inter NIC(Internet Network Information Ce ...