• 之前在网上搜索按时间统计,发现不是很全 ,接着别人的思路进行延伸下,
  • mysql统计天、周、月、季度、半年、年
  • 前期工作创建辅助表

    CREATE TABLE num (i INT);
    INSERT INTO num (i) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  • 创建要查询的表
    CREATE TABLE `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(32) NOT NULL COMMENT '用户名称',
    `sex` char(1) DEFAULT NULL COMMENT '性别',
    `address` varchar(256) DEFAULT NULL COMMENT '地址',
    `create_time` varchar(100) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; -- ----------------------------
    -- Records of user
    -- ----------------------------
    INSERT INTO `user` VALUES ('', '小南', '男', '湖南湘潭', '2019-07-01');
    INSERT INTO `user` VALUES ('', '小谭', '男', '湖南湘潭', '2019-07-02');
    INSERT INTO `user` VALUES ('', '小梅', '女', '广东梅县', '2019-07-01');
    INSERT INTO `user` VALUES ('', '小仪', '女', '四川仪陇', '2019-05-10');
    INSERT INTO `user` VALUES ('', '小蓝', '女', '上海嘉定', '2019-11-11');
    INSERT INTO `user` VALUES ('', '小苍', '男', '湖南湘潭', '2018-06-01');
    INSERT INTO `user` VALUES ('', '小萨', '男', '湖南湘潭', '2018-01-02');
    INSERT INTO `user` VALUES ('', '小静', '女', '广东梅县', '2018-06-01');
    INSERT INTO `user` VALUES ('', '大幂幂', '女', '四川仪陇', '2019-03-10');
    INSERT INTO `user` VALUES ('', '宋小宝', '女', '上海嘉定', '2019-05-11');
  • 按天统计

    SELECT
    temp.date,
    COALESCE (u.unmber, 0) 'number'
    FROM
    (
    SELECT
    adddate('2019-07-01', numlist.id) AS 'date'
    FROM
    (
    SELECT
    n1.i + n10.i * 10 + n100.i * 100 AS id
    FROM
    num n1
    CROSS JOIN num AS n10
    CROSS JOIN num AS n100
    ) AS numlist
    WHERE
    adddate('2019-07-01', numlist.id) < date_add('2019-07-05',INTERVAL 1 DAY)
    ) temp
    LEFT JOIN (
    SELECT
    LEFT (create_time, 10) AS udate,
    count(create_time) unmber
    FROM
    user
    WHERE
    1=1
    GROUP BY
    udate
    ) u ON temp.date = u.udate
    ORDER BY
    temp.date;

  • 按周统计

    SELECT
    temp.monthWeek,
    COALESCE (plan.number, 0) 'number'
    FROM
    (
    SELECT
    YEARWEEK(
    adddate('2019-07-01', INTERVAL numlist.id WEEK),1 ) AS 'date',
    CONCAT(
    MONTH (date_format('2019-07-01', '%Y-%m-%d')),'月第', numlist.id + 1,'周') AS 'monthWeek'
    FROM
    (
    SELECT
    *
    FROM
    (
    SELECT
    n1.i + n10.i * 10 AS id
    FROM
    num n1
    CROSS JOIN num AS n10
    ) a
    WHERE
    a.id <= 11
    ) AS numlist
    WHERE
    adddate('2019-07-01',INTERVAL numlist.id WEEK ) <= '2019-07-31'
    ) temp
    LEFT JOIN (
    SELECT
    YEARWEEK( date_format(create_time, '%Y-%m-%d'), 1) date,
    count( YEARWEEK(date_format(create_time, '%Y-%m-%d'),1 ) ) number
    FROM
    USER
    WHERE
    1 = 1
    AND create_time >= '2019-07-01'
    AND create_time <= '2019-07-31'
    GROUP BY
    YEARWEEK(
    date_format(create_time, '%Y-%m-%d'),
    1
    )
    ) plan ON temp.date = plan.date;

  • 按月统计

    SELECT
    LEFT (temp.date, 7) monthStr,
    COALESCE (u.unmber, 0) 'number'
    FROM
    (
    SELECT
    adddate('2019-01-01',INTERVAL numlist.id MONTH ) AS 'date'
    FROM
    (
    SELECT
    *
    FROM
    (
    SELECT
    n1.i + n10.i * 10 AS id
    FROM
    num n1
    CROSS JOIN num AS n10
    ) a
    WHERE
    a.id <= 11
    ) AS numlist
    WHERE
    adddate('2019-01-01',INTERVAL numlist.id MONTH) <= '2019-12-3'
    ) temp
    LEFT JOIN (
    SELECT
    LEFT (create_time, 7) AS udate,
    count(create_time) unmber
    FROM
    user
    WHERE
    1=1
    GROUP BY
    udate
    ) u ON LEFT (temp.date, 7) = u.udate
    ORDER BY
    temp.date
  • 按季度统计

    SELECT
    temp.monthWeek,
    COALESCE(plan.number, 0) 'number'
    FROM
    (
    SELECT
    QUARTER(adddate('2019-01-01', INTERVAL numlist.id QUARTER )) AS 'date',
    CONCAT(year(date_format('2019-01-01','%Y-%m-%d')), '年第', numlist.id +1, '季度') AS 'monthWeek'
    FROM
    (
    SELECT
    *
    FROM
    (
    SELECT
    n1.i + n10.i * 10 AS id
    FROM
    num n1
    CROSS JOIN num AS n10
    ) a
    WHERE
    a.id <= 11
    ) AS numlist
    WHERE
    adddate('2019-01-01',INTERVAL numlist.id QUARTER) <= '2019-12-31'
    ) temp
    LEFT JOIN (
    SELECT QUARTER(date_format(create_time,'%Y-%m-%d')) date,
    count(QUARTER(date_format(create_time,'%Y-%m-%d'))) number
    FROM user
    where 1=1
    and create_time >= '2019-01-01'
    and create_time <= '2019-12-31'
    group by QUARTER(date_format(create_time,'%Y-%m-%d'))
    ) plan
    on temp.date = plan.date

  • 按半年统计

    SELECT
    bannian 'content',
    COALESCE (u.unmber, 0) 'number'
    FROM
    (
    SELECT
    adddate( '2018-01-01',INTERVAL numlist.id MONTH) AS 'date',
    case DATE_FORMAT(adddate( '2018-01-01',INTERVAL numlist.id MONTH),'%c')
    when 7 then '下半年'
    when 8 then '下半年'
    when 9 then '下半年'
    when 10 then '下半年'
    when 11 then '下半年'
    when 12 then '下半年'
    else '上半年' end
    as bannian
    FROM
    (
    SELECT * FROM(
    SELECT
    n1.i + n10.i * 10 AS id
    FROM
    num n1
    CROSS JOIN num AS n10
    ) a
    WHERE a.id <= 11
    ) AS numlist
    WHERE
    adddate('2018-01-01',INTERVAL numlist.id MONTH) <= '2018-12-31'
    GROUP BY bannian
    ) temp
    LEFT JOIN (
    SELECT
    case DATE_FORMAT(create_time,'%c')
    when 7 then '下半年'
    when 8 then '下半年'
    when 9 then '下半年'
    when 10 then '下半年'
    when 11 then '下半年'
    when 12 then '下半年'
    else '上半年' end as udate,
    count(create_time) unmber
    FROM user
    WHERE 1=1
    and date_format(create_time,'%Y-%m-%d') >='2018-01-01'
    and date_format(create_time,'%Y-%m-%d') <= '2018-12-31'
    GROUP BY udate
    ) u ON bannian = u.udate ORDER BY bannian asc;

  • 按年统计(近五年)

    SELECT
    COALESCE(plan.number, 0) 'number',
    temp.date fiveYear
    FROM
    (
    SELECT
    Year(adddate('2015-01-01', INTERVAL numlist.id Year )) AS 'date'
    FROM
    (
    SELECT
    *
    FROM
    (
    SELECT
    n1.i + n10.i * 10 AS id
    FROM
    num n1
    CROSS JOIN num AS n10
    ) a
    WHERE
    a.id <= 11
    ) AS numlist
    WHERE
    adddate('2015-01-01',INTERVAL numlist.id Year) <= '2019-12-31'
    ) temp
    LEFT JOIN (
    SELECT Year(date_format(create_time,'%Y-%m-%d')) date,
    count(Year(date_format(create_time,'%Y-%m-%d'))) number
    FROM user
    where 1=1
    group by year(date_format(create_time,'%Y-%m-%d'))
    ) plan
    on temp.date = plan.date
    order by temp.date asc

mysql统计天、周、月、季度、半年、年的更多相关文章

  1. mysql按日/周/月统计

    一.mysql按日统计 ) count ' and start_time > '2017-06-28' group by days; 二.mysql按周统计 ) ' group by weeks ...

  2. java获取当前时间的年周月季度等的开始结束时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  3. MySql查询日周月

    常用计算日期的函数 日 date(日期) = CURDATE() 自然周 YEARWEEK(date_format(日期,'%Y-%m-%d') , 1) = YEARWEEK(now() , 1) ...

  4. mysql 查询,天,周,月等写法

    1.查询当天的数据 select * from 表名 where TO_DAYS(时间字段)=TO_DAYS(NOW()); 2.查询当周的数据 select * from 表名 where YEAR ...

  5. mysql 时间戳 按周、日、月 统计方法 附 date格式

    create_time时间戳格式 SELECT FROM_UNIXTIME(create_time,'%Y%u') weeks,COUNT(id) COUNT FROM role GROUP BY w ...

  6. mysql中增加某一时间段内的时间数据(包含:时间、年、月、日、第几周、季度)

    创建表dim_date: create table `dim_date` ( `year` int (20), `month` int (20), `day` int (20), `week` int ...

  7. mysql如何按周统计数据?

    转自:https://www.cnblogs.com/wanghetao/p/3920124.html MySql 按周/月/日统计数据的方法 知识关键词:DATE_FORMAT  select DA ...

  8. pg按日,周,月进行数据统计

    pg数据库按周,月统计数据 SELECT date_trunc('WEEK', insert_time) as insertDate, SUM(data_increment) as dataTotal ...

  9. Java统计用户年/月/周/日网站访问量

    一:准备工作,引入相关依赖: 二:运行效果图: 下一次访问 三:具体代码如下  (1):CountObjectInfo.java package cn.csrc.base.count; import ...

随机推荐

  1. Python 操作Gitlab-API 实现批量的合并分支

    1.需求:每次大批量上线完成后,都会进行将hotfix合并到Master,合并到test/uat等等重复操作(上线发布后自动合并master已完成). 2.现实:在完成发布后自动合并master后,可 ...

  2. 小白学 Python 爬虫(5):前置准备(四)数据库基础

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  3. just_connect.py

    # -*- coding=utf-8 -*-from mysql.connector import connect config = { 'host': '192.168.199.139', 'por ...

  4. ctf比赛linux文件监控和恢复shell

    之前参加ctf比赛时候临时写的,有很多不足,不过可以用,就贴出来分享给大家,希望对大家有帮助. 脚本一:记录当前目录情况 #!/bin/bashfunction getdir(){    for el ...

  5. 基于 HTML5 + WebGL 的宇宙(太阳系) 3D 可视化系统

    前言 近年来随着引力波的发现.黑洞照片的拍摄.火星上存在水的证据发现等科学上的突破,以及文学影视作品中诸如<三体>.<流浪地球>.<星际穿越>等的传播普及,宇宙空间 ...

  6. 如何使用JavaScript直接上传并预览粘贴板的图片?

    (题图:梵高-橄榄树) 提出需求 因为工作原因,现在有一个需求就是需要用户使用QQ或者微信复制一张截图后,在div中直接粘贴这张图片,而不是采用上传的方式.类似我们在使用QQ微信时直接粘贴截图的操作, ...

  7. 【前端】之JavaScript基础知识

    JS 基础知识 JS中,简单类型的数据存储在栈中,复杂类型的数据存储在堆中,其引用存储在栈中 JS中的深拷贝和浅拷贝: 浅拷贝:将对象中的所有简单类型的属性拷贝出来,引用类型属性直接赋值null 深拷 ...

  8. Linux命令行初学(一)

    linux命令大全:https://www.linuxcool.com/ 大概了解到有哪些命令,如果有需要的话可以在该网站上查询. 另外在实验楼学习了一些基础,该篇博客就此次对linux命令行的学习进 ...

  9. ThinkPHP5——Session和Cookie

    Session 首先要引入use think\Session:下面使用静态方法调用Session //赋值 Session::set('name','s1'); //赋值think作用域,set(‘名 ...

  10. js2——定时跳转

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...