mysql查询最近半年数据,本季度,本年

1 、 查看当天日期

select current_date();

2、 查看当天时间

select current_time();

3、查看当天时间日期

select current_timestamp();

4、查询当天记录

select * from 表名 where to_days(时间字段名) = to_days(now());

5、查询昨天记录

select * from 表名 where to_days( now( ) ) – to_days( 时间字段名) <= 1

6、查询7天的记录

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名) 

7、查询近30天的记录

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(时间字段名)

8、查询本月的记录

select * from 表名 where date_format( 时间字段名,%y%m’ ) = date_format( curdate( ) ,%y%m’ )

9、查询上一月的记录

select * from 表名 where period_diff( date_format( now( ) ,%y%m’ ) , date_format( 时间字段名,%y%m’ ) ) =1

10、查询本季度数据

select * from 表名 where quarter(create_date)=quarter(now());

11、查询上季度数据

select * from 表名 where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

12、查询本年数据

select * from 表名  where year(create_date)=year(now());

13、查询上年数据

select * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year));

14、查询当前这周的数据

select * from 表名 where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());

15、查询上周的数据

select * from 表名 where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;

16、查询当前月份的数据

select * from 表名   where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')

17、查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

转载:
https://josh-persistence.iteye.com/blog/2128275

https://blog.csdn.net/qq_29540647/article/details/79985771

https://stackoverflow.com/questions/7568072/subtract-month-and-day-mysql

  • 2
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值