1.建表

CREATE TABLE `t_log_code_num` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`server_id` int(3) NOT NULL,

`date` date NOT NULL,

`code` varchar(20) NOT NULL,

`num` int(10) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

2.录入数据

INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘1‘, ‘2017-07-31‘, ‘201058‘, ‘1‘);

INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘2‘, ‘2017-07-31‘, ‘201612‘, ‘2113‘);

INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘1‘, ‘2017-07-30‘, ‘201058‘, ‘1‘);

INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘2‘, ‘2017-07-30‘, ‘201314‘, ‘310‘);

INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘1‘, ‘2017-07-29‘, ‘201322‘, ‘1890‘);

INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘2‘, ‘2017-07-29‘, ‘201203‘, ‘379‘);

3.数据显示

4.统计sql

select t.`code`,

sum(case t.date when ‘2017-07-29‘ then t.num else 0 end ) as ‘2017-07-29‘,

sum(case t.date when ‘2017-07-30‘ then t.num else 0 end ) as ‘2017-07-30‘,

sum(case t.date when ‘2017-07-31‘ then t.num else 0 end ) as ‘2017-07-31‘

from (

select date,code, sum(num) num from t_log_code_num GROUP BY date,code

) t GROUP BY t.`code`;

5.统计结果

代码如下:

CREATE TABLE`t_log_code_num` (

`id`int(11) NOT NULLAUTO_INCREMENT,

`server_id`int(3) NOT NULL,

`date` dateNOT NULL,

`code`varchar(20) NOT NULL,

`num`int(10) NOT NULL,PRIMARY KEY(`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘1‘, ‘2017-07-31‘, ‘201058‘, ‘1‘);INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘2‘, ‘2017-07-31‘, ‘201612‘, ‘2113‘);INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘1‘, ‘2017-07-30‘, ‘201058‘, ‘1‘);INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘2‘, ‘2017-07-30‘, ‘201314‘, ‘310‘);INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘1‘, ‘2017-07-29‘, ‘201322‘, ‘1890‘);INSERT INTO t_log_code_num (`server_id`, `date`, `code`, `num`) VALUES (‘2‘, ‘2017-07-29‘, ‘201203‘, ‘379‘);SELECT * fromt_log_code_num;selectt.`code`,sum(case t.date when ‘2017-07-29‘ then t.num else 0 end ) as ‘2017-07-29‘,sum(case t.date when ‘2017-07-30‘ then t.num else 0 end ) as ‘2017-07-30‘,sum(case t.date when ‘2017-07-31‘ then t.num else 0 end ) as ‘2017-07-31‘

from(select date,code, sum(num) num from t_log_code_num GROUP BYdate,code

) tGROUP BY t.`code`;

原文:http://www.cnblogs.com/zhouj/p/7269930.html

mysql 单列转换为行,mysql行转换为列相关推荐

  1. mysql中将列动态转换为行,mysql 行转列 MySQL数据库动态行转列

    想把mysql一个表的行转成列,图1是原表,想实现图2的样式SELECT MAX(CAS就是一个动态的行列转换 CREATE TABLE `c_wssb_zz` ( `aa011` varchar(1 ...

  2. 在SQL Server中将行有效地转换为列

    本文翻译自:Efficiently convert rows to columns in sql server I'm looking for an efficient way to convert ...

  3. Mysql框架<二> 行式存储与列式存储

    文章目录 数据处理场景分类 OLTP 联机事务处理 OLAP 联机分析处理 行式存储和列式存储 概念 特点 对比 数据处理场景分类 OLTP 联机事务处理 OLTP : on-line transac ...

  4. 周末杂谈:在MYSQL中,添加数据行后,使用右外连接查询所有课程的选修情况,要求列出课程编号、课程名称、选修人数,表中没有的课程列值为空的解决方法

    周末杂谈:在MYSQL中,添加数据行后,使用右外连接查询所有课程的选修情况,要求列出课程编号.课程名称.选修人数,表中没有的课程列值为空的解决方法 实验前期准备 course表(课程表) 在其中: c ...

  5. Mysql来帮忙:多行合并成一列

    先还是来说下业务场景 现在有个业务需求,需要根据父类ID(pid),分别找出3条最新的数据展示出来. 如果使用group by发现每个只能显示一条. 那么怎么办? 接下来就是解决现场. 解决 解决思路 ...

  6. 一千行mysql笔记

    原文地址:https://shockerli.net/post/1000-line-mysql-note/ 1 /* Windows服务 */ 2 -- 启动MySQL 3 net start mys ...

  7. 『转』一千行MySQL学习笔记

    1 /* 启动MySQL */ 2 net start mysql 3 4 /* 连接与断开服务器 */ 5 mysql -h 地址 -P 端口 -u 用户名 -p 密码 6 7 /* 跳过权限验证登 ...

  8. mysql concat 不同字符集_字符编码 - 如何将整个MySQL数据库字符集和排序规则转换为UTF-8?...

    字符编码 - 如何将整个MySQL数据库字符集和排序规则转换为UTF-8? 如何将整个MySQL数据库字符集转换为UTF-8并将整理转换为UTF-8? 17个解决方案 591 votes 使用utf8 ...

  9. 命令开启 停止mysql服务_windows 下命令行启动停止mysql

    MySQL比较好玩一点就是它可以用多种方式启动,当然它也可以用多种方式关闭.下面我就mysql的几种启动方式简单的谈一谈,希望可以给大家提供一些参考. 第一种,用mysqld-nt来启动. 在没有进行 ...

  10. mysql left join超时,MySQL 行锁超时排查方法优化

    一.大纲 #### 20191219 10:10:10,234 | com.alibaba.druid.filter.logging.Log4jFilter.statementLogError(Log ...

最新文章

  1. SQL关键字转换大写核心算法实现
  2. 鸿蒙2.0操作体验,鸿蒙2.0操作系统正式版-华为鸿蒙2.0操作系统正式版官方预约 v1.0.0-优盘手机站...
  3. Linux下FTP服务器搭建
  4. XCTF_Web_新手练习区:command_execution
  5. java云题库测试使用说明 0917
  6. 【C语言】数据结构C语言版 实验5 递归
  7. 纯CSS实现下拉菜单导航
  8. MySQL OCP认证(文末附参考题)
  9. 关于使用UE编辑器无法初始化ftp的问题
  10. [学习笔记]周跳探测
  11. 【人脸表情识别】基于matlab GUI微表情识别系统【含Matlab源码 1852期】
  12. 免ROOT卸载手机自带软件详细教程
  13. 最近计算机速度测试情况,WiFi速度测试测试测试互联网速度多少兆字节
  14. Elasticsearch教程 | 第三篇:审计设置
  15. python -m详解
  16. 利用google or-tools 求解逻辑难题:斑马问题
  17. 高德地图自己录制导航声音备份及恢复方法
  18. java—JSON(课堂笔记篇)
  19. 许亲亲--1101210998 第二次作业
  20. 芥酸油酯的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告

热门文章

  1. c语言:找出1到4000中,数字的各位数之和能被4整除的数有多少个?
  2. BZOJ4298 : [ONTAK2015]Bajtocja
  3. jquery mobile的a标签点击无法跳转的问题
  4. 系统内存信息获取工具类
  5. 绿色版mysql安装步骤
  6. NameValueCollection详解
  7. (linux)查看及修改文件权限以及相关
  8. 比特币现金(BCH),存在网络上的隐形矿工哈希率分布
  9. eclipse svn提交忽略文件及文件夹,ignore设置无效..
  10. Untiy3D学习笔记记录