当前位置:网站首页>MySQL中, 如何查询某一天, 某一月, 某一年的数据
MySQL中, 如何查询某一天, 某一月, 某一年的数据
2022-07-07 14:02:00 【aGreetSmile】
MySQL中, 如何查询某一天, 某一月, 某一年的数据
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
近7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
查询本季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
查询上季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
查询本年数据
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查询上个月的数据
复制代码
复制代码
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from user where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())
select * from user where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select * from user where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select * from user where pudate between 上月最后一天 and 下月第一天
复制代码
复制代码
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询某个月的数据(查询17年10月份数据)
select * from exam where date_format(starttime,'%Y-%m')='2017-10'
select * from exam where date_format(starttime,'%Y-%m')=date_format('2017-10-05','%Y-%m')
边栏推荐
- 121. The best time to buy and sell stocks
- TS typescript type declaration special declaration field number is handled when the key key
- 分步式监控平台zabbix
- The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
- Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
- 无线传感器网络--ZigBee和6LoWPAN
- Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
- AE learning 02: timeline
- Step by step monitoring platform ZABBIX
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
猜你喜欢
Application example of infinite list [uigridview]
numpy---基础学习笔记
Continuous creation depends on it!
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
20th anniversary of agile: a failed uprising
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Annexb and avcc are two methods of data segmentation in decoding
【微信小程序】Chapter(5):微信小程序基础API接口
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
随机推荐
How to implement backspace in shell
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
AE learning 02: timeline
Function: JS Click to copy content function
numpy--数据清洗
AB package details in unity (super detail, features, packaging, loading, manager)
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
Unity drawing plug-in = = [support the update of the original atlas]
Mesh merging under ue4/ue5 runtime
Syntax of generator function (state machine)
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Asynchronous application of generator function
There are many ways to realize the pause function in JS
Three. JS introductory learning notes 04: external model import - no material obj model
A JS script can be directly put into the browser to perform operations
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
U3D_ Infinite Bessel curve