Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6152152
  • 博文数量: 498
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6740
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(498)

文章存档

2020年(55)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: Python/Ruby

2020-11-06 18:33:28

1.今天

SELECT * FROM 表名 WHERE TO_DAYS(时间字段名) = TO_DAYS(NOW());

2.昨天

SELECT * FROM 表名 WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段名) <= 1;

3.本周

SELECT * FROM 表名 WHERE YEARWEEK(DATE_FORMAT(时间字段名,'%Y-%m-%d')) = YEARWEEK(NOW());

4.上周

SELECT * FROM 表名 WHERE YEARWEEK(DATE_FORMAT(时间字段名,'%Y-%m-%d')) = YEARWEEK(NOW())-1;

5.近7天

SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(时间字段名);

6.近30天

SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(时间字段名);

7.本月

SELECT * FROM 表名 WHERE DATE_FORMAT(时间字段名,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m');

8.上月

复制代码
SELECT * FROM 表名 WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(时间字段名,'%Y%m')) = 1;

SELECT * FROM 表名 WHERE DATE_FORMAT(时间字段名,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; 

SELECT * FROM 表名 WHERE WEEKOFYEAR(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = WEEKOFYEAR(NOW()); 

SELECT * FROM 表名 WHERE MONTH(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = MONTH(NOW()); 

SELECT * FROM 表名 WHERE YEAR(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = YEAR(NOW()) AND MONTH(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = MONTH(NOW());
复制代码

9.近6个月

SELECT * FROM 表名 WHERE 时间字段名 BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW();

10.本季度

SELECT * FROM 表名 WHERE QUARTER(时间字段名) = QUARTER(NOW());

11.上季度

SELECT * FROM 表名 WHERE QUARTER(时间字段名) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER));

12.本年

SELECT * FROM 表名 WHERE YEAR(时间字段名)=YEAR(NOW());

13.去年

SELECT * FROM 表名 WHERE YEAR(时间字段名) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));


python代码示例

点击(此处)折叠或打开




  1. class PeriodType(object):
        WEEK = 1
        WEEK_PRE = 2
        MONTH = 3
        MONTH_PRE = 4
        QUARTER = 5
        QUARTER_PRE = 6
        YEAR = 7
        YAER_PRE = 8


  2. def get_created_at_sql_cons(table_name, period):
  3.     sql_filter = ""
  4.     if PeriodType.WEEK == period:
  5.         sql_filter = "where yearweek(date_format(%s.created_at, '%y-%m-%d')) = yearweek(now())" % (table_name)
  6.     elif PeriodType.WEEK_PRE == period:
  7.         sql_filter = "where yearweek(date_format(%s.created_at, '%y-%m-%d')) = yearweek(now())-1" % (table_name)
  8.     elif PeriodType.MONTH == period:
  9.         sql_filter = "WHERE DATE_FORMAT(%s.created_at, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')" % (table_name)
  10.     elif PeriodType.MONTH_PRE == period:
  11.         sql_filter = "WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(%s.created_at,'%Y%m')) = 1" % (
  12.             table_name)
  13.     elif PeriodType.QUARTER == period:
  14.         sql_filter = "WHERE QUARTER(%s.created_at)" % (table_name)
  15.     elif PeriodType.QUARTER_PRE == period:
  16.         sql_filter = "WHERE QUARTER(%s.created_at) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER))" % (table_name)

  17.     elif PeriodType.YEAR == period:
  18.         sql_filter = "WHERE YEAR(%s.created_at)=YEAR(NOW())" % (table_name)

  19.     elif PeriodType.YAER_PRE == period:
  20.         sql_filter = "WHERE YEAR(%s.created_at)=YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" % (table_name)
  21.     else:
  22.         pass
  23.     return sql_filter


阅读(93) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~