mysql 您要的日期查询都在这

在这里插入图片描述


?? 主页:
?? 感谢各位大佬 点赞?? 收藏 留言?? 加关注!
?? 收录于专栏:数据库工程师


文章目录

一、??前言

本文介绍了mysql常用日期查询的方法。希望能帮助到您。

二、??常见日期查询方法

1、??查询今天数据

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

2、??查询昨天数据

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

3、??查询近7天数据

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

4、??查询近30天数据

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

5、??查询当前月数据

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

6、??查询上一月数据

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

7、??查询本季度数据

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

8、??查询上季度数据

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

9、??查询本年数据

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

10、??查询上年数据

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

11、??查询当前周的数据

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

12、??查询上周的数据

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

13、??查询上个月的数据

select * from 表名 where date_format(create_date,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

14、??查询当前月份的数据

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

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

select * from 表名 where create_date between date_sub(now(),interval 6 month) and now();

16、??查询某一天所在周的第一天

select
case when dayname(date('2022-3-10'))='sunday'
then date_sub(date('2022-3-10'),interval 6 day)
else date_add('2022-3-10',interval -dayofweek(date('2022-3-10'))+2 day) end

17、??查询某一天所在周的最后一天

select case when dayname(date('2022-3-11'))='sunday' then date('2022-3-11') else date_add('2022-3-11',interval 7-dayofweek('2022-3-11')+1 day) end

18、??查询某一天的所在月的第一天

select date_add( date_add(last_day('2022-06-03'),interval 1 day ),interval -1 month );

19、??查询某一天所在月的最后一天

select last_day('2022-03-03');
select date_format(now(),'%y-%m-%d %h:%i:%s');

20、??查询某一天所在月的天数

select timestampdiff(day,'2023-03-03',(date_add('2017-03-03',interval 1 month)));

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值