datetime mysql 当天_mysql 查询当日,昨日,当月,当季度,上一季度和当天某一时间段的数据...

test:表名

CreateDateTime:时间字段名

1. 获取当日的数据

select * from test where to_days(CreateDateTime)= to_days(now())

2. 获取昨日的数据

select * from test where to_days(now())-todays(CreateDateTime) <=1

3. 获取当月的数据

select * from test where date_format(CreateDateTime, ‘%Y%m’ ) = date_format( curdate() , ‘%Y%m’ )

4. 查询上一个月的数据

select * from test where period_diff(date_format(now(),’%Y%m’),date_format(CreateDateTime,’%Y%m’)) =1

5. 获取当季度的数据

select * from test where quarter(CreateDateTime)=quarter(now())

6. 获取上一个季度的数据

select * from test where quarter(CreateDateTime)=quarter(date_sub(now(),interval 1 quarter))

7. 查询当天某一时间段的数据

select * from test where UNIX_TIMESTAMP( CreateDateTime) BETWEEN UNIX_TIMESTAMP(CURDATE())+3600*10 AND UNIX_TIMESTAMP(CURDATE())+3600*20

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值