zoukankan      html  css  js  c++  java
  • mysql行转换为列

    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 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;
    
    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 * from t_log_code_num;
    
    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`;
  • 相关阅读:
    Maven的settings.xml配置详解
    Activiti创建表(三)
    解决报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized
    解决报错(Could not create connection to database server.)
    Activiti插件安装(二)
    2018年四川理工学院软件工程考试大纲(软件工程概述)
    通过myclipse建立一个简单的Hibernate项目(PS:在单元测试中实现数据的向表的插入)
    MySQL如何永久解决由dos编码格式导致MySQ的显示乱码
    nyoj 209 + poj 2492 A Bug's Life (并查集)
    nyoj 208 + poj 1456 Supermarket (贪心)
  • 原文地址:https://www.cnblogs.com/zhouj/p/7269930.html
Copyright © 2011-2022 走看看