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 ...
随机推荐
- <!>字体效果
<h1>...</h1>标题字(最大) <h6>...</h6>标题字(最小) <b>...</b>粗体字 <strong ...
- mfc小工具开发之定时闹钟之---功能介绍
使用背景: 之前在xp上用过飞雪日历,感觉挺好用的,还有在音频上的兴趣,促使了我也要自己做一个简单的定时闹钟. 之前开发过图片格式的小工具,没来的急分享,后期整理后,一块奉上,写这篇介绍的时候已近完成 ...
- iOS开发中“此证书的签发者无效”的解决方式
iOS开发过程中有时候会出现证书所有变成无效,例如以下图 然后进行打包的时候会提演示样例如以下警告: 解决方法: 第一步: 下载https://developer.apple.com/certif ...
- 卡友pos机使用流程
Q: pos机正常使用步骤 A: 1. 按开机键开机2. 输入“01”进行签到3. 系统提示输入密码,密码为“0000”4. 系统提示“请刷卡”,可正常刷卡消费首次使用请务必登陆商户后台核对结算收款账 ...
- Chem 3D模型的参数值更改方法
在化学绘图软件ChemOffice 15.1中有个专门用于绘制三维结构的组件,就是Chem 3D.通过这个组件用户可以绘制3D模型并可以通过这个组件来计算一些化学数据.在使用Chem 3D组件过程中, ...
- 存储过程根据ouID获取IntlPerson数据表
/****************************************************************************** ** Name: usp_base_Ge ...
- JS语法快速查询
本文转载至 http://wenku.baidu.com/link?url=z4gND-0w-Cq7hkn2Vnnz0CAJJPwJ8jJrFY0jtnnACiaz4yMK49VAvfJ3BlTVcm ...
- Java中匿名内部类
匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:不使用匿名内部类来实现抽象 ...
- HTML-CSS文件链接HTML的三种方式
<!--css文本的链接方式有三种:分别是内联定义.链入内部css.和链入外部css--> <!--1.代码为:--> <!--<html> <head ...
- Django - 环境搭建、url、视图、模板、标签、过滤器
(一).简介 简介就不多说了,网上的内容一大堆.总结来说,django是走大而全的路线,写项目超级快,几乎什么都为你考虑到了,你就乖乖照着它的格式来写就行了. 这里来一些基本认知: web应用框架(w ...