当前位置:网站首页>MySql查询某个时间段内的数据(前一周、前三个月、前一年等)
MySql查询某个时间段内的数据(前一周、前三个月、前一年等)
2022-08-02 21:51:00 【模糊化的】
1、使用函数DATE_SUB
DATE_SUB函数在当前时间减去一定的时间
DATE_SUB(date,INTERVAL expr unit)
date:合法日期,可以是当前时间now()
expr:希望减去的时间,整数
unit:减去时间的单位,如day,week,month,year
实例:
u_user表
(1)、前一周的数据
当前时间:
sql:
select * from u_user where u_create_time > DATE_SUB(NOW(),INTERVAL 7 day);
结果:
另一种sql写法:
select * from u_user where u_create_time > DATE_SUB(NOW(),INTERVAL 1 WEEK);
(2)、前三个月的数据
sql:
select * from u_user where u_create_time > DATE_SUB(NOW(),INTERVAL 3 MONTH);
结果:
(3)、前一年的数据
sql:
select * from u_user where u_create_time > DATE_SUB(NOW(),INTERVAL 1 YEAR);
结果:
2、使用函数DATE_ADD
DATE_ADD函数在当前时间加上一定的时间
DATE_ADD(date,INTERVAL expr unitda
date:合法日期,可以是当前时间now(),也可以是表中的时间字段
expr:希望加上的时间,整数
unit:加上时间的单位,如day,week,month,year
实例:
(1)、前一周的数据
sql:
select * from u_user where DATE_ADD(u_create_time,INTERVAL 7 DAY) > NOW();
结果:
(2)、前三个月的数据
sql:
select * from u_user where DATE_ADD(u_create_time,INTERVAL 3 MONTH) > NOW();
结果:
(3)、前一年的数据
sql:
select * from u_user where DATE_ADD(u_create_time,INTERVAL 1 YEAR) > NOW();
结果:
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 浅读一下dotenv的主干逻辑的源码
- 四、字符常量 & 字符串
- 不堪哥哥殴打谩骂,妹妹申请人身安全保护令获支持
- “百日行动”进行时:700余交通安全隐患被揪出
- LeetCode 2359. 找到离给定两个节点最近的节点 基环树
- 成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
- What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
- # 医院管理系统完整项目代码以及数据库建表语句分享
- JS Date 时间戳 getTune data.parse 倒计时小程序
- 面试官居然问我:删库后,除了跑路还能干什么?
猜你喜欢
ROS2初级知识(9):bag记录过程数据和重放
GameStop NFT 市场分析
Web APIs BOM- 操作浏览器-Window对象
从月薪10k到30k的必走之路:自动化测试
What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
kubernetes pod podsecurityPolicies(PSP)
Win10怎么开启自带的游戏录屏功能?
【TypeScript】深入学习TypeScript类(下)
LeetCode 2359. 找到离给定两个节点最近的节点 基环树
搭建Spark开发环境(第二弹)
随机推荐
@Transactional 事务调用与生效场景总结
You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
JumpServer open source bastion machine completes Loongson architecture compatibility certification
从月薪10k到30k的必走之路:自动化测试
【Unity】Unity开发进阶(六)UnityEvent使用与源码解析
ROS2初级知识(9):bag记录过程数据和重放
Interviewer: can you talk about optimistic locking and pessimistic locks
搭建直播平台,使用node生成验证码图片,并进行验证
H.265视频流媒体播放器EasyPlayer.js集成时出现“SourceBuffer ”报错,该如何解决?
[TypeScript] Deep Learning of TypeScript Classes (Part 1)
Abstract Factory Pattern
What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
面试了个985毕业的,回答“性能调优”题时表情令我毕生难忘
js function anti-shake and function throttling and other usage scenarios
命令行启动常见问题及解决方案
Byte's internal technical map is amazing and practical
虚拟内存 virualmemory
【DEBUG】ImportError: Unable to import required dependencies: numpy: DLL load failed: 找不到指定的模块。
Web APIs BOM- 操作浏览器-Window对象
How many ways do you know the singleton pattern?