IT俱乐部 MySql mysql数据库您要的常见日期查询方法总结

mysql数据库您要的常见日期查询方法总结

一、前言

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

二、常见日期查询方法

1、查询今天数据

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

2、查询昨天数据

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

3、查询近7天数据

1
select * from 表名 where date_sub(curdate(), interval 7 day)

4、查询近30天数据

1
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY)

5、查询当前月数据

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

6、查询上一月数据

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

7、查询本季度数据

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

8、查询上季度数据

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

9、查询本年数据

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

10、查询上年数据

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

11、查询当前周的数据

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

12、查询上周的数据

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

13、查询上个月的数据

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

14、查询当前月份的数据

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

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

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

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

1
2
3
4
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、查询某一天所在周的最后一天

1
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、查询某一天的所在月的第一天

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

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

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

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

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

附:日期函数解析

date_format()

格式化日期格式,“date_format(时间戳,时间格式)”

date_sub()

从日期减去指定的时间间隔;函数形式DATE_SUB(date,INTERVAL expr type),date 参数是合法的日期表达式。expr 参数是您希望添加的时间间隔,时间间隔参数非常全面,常用的为 年月日时分秒;

1
2
3
4
//减天数
date_sub(时间,INTERVAL 1 DAY)
//减月份
date_sub(时间,INTERVAL 1 MONTH)

yearweek()

返回指定的日期是哪一年的哪个星期,函数形式为 YEARWEEK(date[,mode]) ,其中date的格式一般为‘年-月-日’,mode为1代表一个星期从星期一开始;

weekday()

接受1个参数,即DATE或DATETIME值,函数形式为WEEKDAY(date);返回一个整数,范围从0到6,表示星期一到星期日,即星期一为0,星期二为1,星期日为6;

period_diff()

返回两个时间相差的月份数,注意它是前一个时间减后一个时间。

注意: period1和period2的格式应相同。

1
SELECT PERIOD_DIFF(201710, 201703);

总结 

到此这篇关于mysql数据库常见日期查询方法的文章就介绍到这了,更多相关mysql日期查询方法内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/database/mysql/15500.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部