美文网首页@IT·互联网程序员大数据 爬虫Python AI Sql
MYSQL查询今天,昨天,这个周,上个周,这个月,上个月,今年,

MYSQL查询今天,昨天,这个周,上个周,这个月,上个月,今年,

作者: 帅大叔的简书 | 来源:发表于2017-04-17 10:30 被阅读195次

一般后台做报表什么的,可能会用到

createTime ---- 创建时间, 就是你要对比的时间,表的字段类型为 datetime

直接上代码

-- 查询上周的数据 
-- SELECT count(id) as count FROM user WHERE YEARWEEK(date_format(createTime,'%Y-%m-%d')) = YEARWEEK(now())-1; 

-- 查询这个周的数据
-- SELECT count(id) as count FROM user WHERE YEARWEEK(date_format(createTime,'%Y-%m-%d')) = YEARWEEK(now())

-- 查询上个月的数据 
-- select count(id) as count from user where date_format(createtime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') 

-- 查询这个月的数据 
-- SELECT count(id) as count FROM user WHERE date_format(createtime,'%Y-%m')=date_format(now(),'%Y-%m');
-- select count(id) as count from `user` where DATE_FORMAT(createtime,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; 

-- 查询距离当前现在6个月的数据 
-- select count(id) as count from user where createtime between date_sub(now(),interval 6 month) and now(); 

-- 查询今天的数据
-- SELECT count(id) as count FROM user WHERE date_format(createtime,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d');

-- 查询昨天的数据
-- SELECT * FROM user WHERE TO_DAYS(NOW())-TO_DAYS(createTime) = 1

-- 今年的
-- select * from `user` where YEAR(createTime)=YEAR(NOW());
-- 去年的
-- select * from `user` where YEAR(createTime)=YEAR(NOW())-1;

-- 来一发集合的
select 
    t1.count as toDay,
    tt1.count as lastDay,
    t2.count as lastWeek,
    tt2.count as toWeek,
    t3.count as lastMonth,
    tt3.count as toMonth,
    t4.count as toYear,
    tt4.count as lastYear,
    t.count as total
    from 
(SELECT count(id) as count FROM user WHERE date_format(createtime,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')) t1,
(SELECT count(id) as count FROM user WHERE TO_DAYS(NOW())-TO_DAYS(createTime) = 1) tt1,
(SELECT count(id) as count FROM user WHERE YEARWEEK(date_format(createTime,'%Y-%m-%d')) = YEARWEEK(now())-1) t2,
(SELECT count(id) as count FROM user WHERE YEARWEEK(date_format(createTime,'%Y-%m-%d')) = YEARWEEK(now())) tt2,
(select count(id) as count from user where date_format(createtime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')) t3,
(SELECT count(id) as count FROM user WHERE date_format(createtime,'%Y-%m')=date_format(now(),'%Y-%m')) tt3,
(select count(id) as count from `user` where YEAR(createTime)=YEAR(NOW())) t4,
(select count(id) as count from `user` where YEAR(createTime)=YEAR(NOW())-1) tt4,
(select count(id) as count from user) t

统计当前月,后12个月,各个月的数据

下面是创建对照视图

CREATE
    ALGORITHM = UNDEFINED 
    DEFINER = `tyro`@`%` 
    SQL SECURITY DEFINER
VIEW `past_12_month_view` AS
    SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') AS `month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') AS `month`

然后和你想要统计的表进行关联查询,如下的demo

select 
    v.month,
    ifnull(b.minute,0) count 
from 
    past_12_month_view v 
left join 
(select DATE_FORMAT(t.createTime,'%Y-%m') month,count(t.id) minute  from user t  group by month) b 
on 
    v.month = b.month 
group by 
    v.month

结果如下

QQ图片20170418160557.png
顺便把我上次遇到的一个排序小问题也写出来

数据表有一个sort_num 字段来代表排序,但这个字段有些值是null,

现在的需求是

返回结果集按升序返回,如果sort_num 为null 则放在最后面

mysql null 默认是最小的值,如果按升序就会在前面

解决方法

SELECT * from table_name 
ORDER BY 
  case 
WHEN 
  sort_num is null 
  then 
    1 
  else 0 end, sort_num asc

搞定收工。。。。。

相关文章

  • MYSQL查询今天,昨天,这个周,上个周,这个月,上个月,今年,

    一般后台做报表什么的,可能会用到 createTime ---- 创建时间, 就是你要对比的时间,表的字段类型为 ...

  • mysql 查询当前月份 上个月份 上上个月份

    //查看本月数据 SELECT * FROM content_publish WHERE date_format(...

  • MySQL根据时间查询

    MySQL查询本周、上周、本月、上个月份数据的sql代码查询当前这周的数据SELECT name,submitti...

  • iOS日历

    iOS日历:获取昨天, 一周前的今天 ,两周前的今天 ,上个月最后一天 iOS 13位(毫秒)时间戳输出

  • 上个月

    上个月 已经过去了 我并没有赶上 你的悲伤 雨可以下的很大 就像你夜晚时 情绪裂口的痛楚 平庸挣扎着开出 一朵白色...

  • 上个月

    我们去天上玩蹦床吧

  • 12月17日SEM项目实战笔记

    一,账户情况: 二,同比:是年,去年和今年,去年同期 环比:是月或者周,上个月和这个月 SEM实战点评 一、文档的...

  • MySql 之 查询

    工作中经常 遇到时间类的查询,比如上个月,这个星期,签到之类的,因此写下来有备无患 1查询这一个周的内容 SELE...

  • 2020注定是泡汤年,但起码我们可以赚到这些

    今年注定是泡汤年:上个月的春节泡汤了,这个月的旅游泡汤了。 3月的第二周,确定泡汤的项目有:意甲、NBA、西甲、法...

  • 说说我家的小可爱

    这个题目刚好是在上个月的今天定下来的。上个月,也就是四月二号,因为疫情我们这边停课一周。 小可爱不知哪来的兴趣,天...

网友评论

    本文标题:MYSQL查询今天,昨天,这个周,上个周,这个月,上个月,今年,

    本文链接:https://www.haomeiwen.com/subject/fxxtzttx.html