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 ...
随机推荐
- Windows编程中回调函数的使用心得(MFC篇)
回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定 ...
- 【BZOJ】3394: [Usaco2009 Jan]Best Spot 最佳牧场(floyd)
http://www.lydsy.com/JudgeOnline/problem.php?id=3394 裸的floyd.. #include <cstdio> #include < ...
- 天地币:所用到的 Android Socket 通讯编程技术试验
1.为了开发"天地币"这个Android手机项目,须要用到Socket编程. 2.天地币是一种类似于比特币的虚拟货币. 3.为了赚取CSDN的C币,须要写篇博客. 4.干脆将调试S ...
- CopyOnWriteArrayList分析
ArrayList是比较常用的一个可变大小的数组集合,但是是不能同步的.如果多个线程同时访问一个ArrayList实例,其中至少一个线程从结构上修改了列表,那么它必须保持外部同步.一般通过加锁对象进行 ...
- Domino移动Web上传的附件到RichText域
只是从网上拷贝下来,没有测试. 得到上传文件的路径http://searchdomino.techtarget.com/tip/Trap-an-attachment-path-via-the-Domi ...
- AndroidManifest.xml文件详解(activity)(一)
<activity android:allowTaskReparenting=["true" | "false"] android:alwaysRetai ...
- spring中关于FeignClient的错误 QueryParam.value() was empty on parameter 1
现创建一个feignClient的接口,在其他服务调用改client时发现服务启动失败错误日志如下: org.springframework.beans.factory.BeanCreationExc ...
- git 拉取远程分支到本地并建立关联关系
git拉取远程分支到本地 一.查看远程分支 使用如下git命令查看所有远程分支: git branch -r 二.拉取远程分支并创建本地分支 方法一 使用如下命令: git checkout ...
- MySQL中的注释(有三种)
MysQL支持三种注释: .#... (推荐这种,具有通性) ."-- ..." (注意--后面有一个空格) ./*...*/
- Powershell & SQL
一.使用Powershell插入数据到SQL中 1.方法一 $ServerName=xxx $Database=xxx $UserID=xxx $Pwd=xxx $conn=new-object Sy ...