惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - darkblue

mysql官方资源 mysql-用户管理 mysql备份还原 水液代谢与五脏调节 津液 献给成长中的孩子们 oracle--v$lock type字段详解 iis日志字段解析 Asp.net Core学习文章 如何让vs2017 EF实体生成支持Mysql 和 Oracle? 连接sqlexpress mysql-root本地无法登录处理 阿里云乌班图16配置-PHP环境(包括mysql及apache安装) mysql主从复制跳过错误 64位系统下powerdesigner15连接oracle odbc 解决“指定的服务已经标记为删除”问题 mysql系列-安装及服务启动 数据缓存管理 redis-在乌班图下设置自动启动
MYSQL时间查询相关
darkblue · 2021-02-04 · via 博客园 - darkblue

0.创建表sql语句查询

mysql> show create table byzp_personinfo;

CREATE TABLE `byzp_personinfo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `birthday` date NOT NULL,
  `create_data` datetime(6),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 |

1.今天

select * from byzp_personinfo where to_days(birthday) <= to_days(now());

2.昨天

select * from byzp_personinfo where to_days(NOW()) - TO_DAYS(birthday) <= 1;

3.近7天

select * from byzp_personinfo where date_sub(CURDATE(),INTERVAL 7 DAY) <= DATE(birthday);  

4.近30天

select * from byzp_personinfo where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(birthday);

5.本月

select * from byzp_personinfo WHERE DATE_FORMAT( birthday, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' );

6.上一月

select * from byzp_personinfo WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( birthday, '%Y%m' ) ) =1; 

7.查询本季度数据

select * from byzp_personinfo where QUARTER(birthday)=QUARTER(now()); 

8.查询上季度数据

select * from byzp_personinfo where QUARTER(birthday)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));  

9.查询本年数据

select * from byzp_personinfo where YEAR(birthday)=YEAR(NOW());  

10.查询上年数据

select * from byzp_personinfo where year(birthday)=year(date_sub(now(),interval 1 year));  

11.查询距离当前现在6个月的数据

select * from byzp_personinfo where birthday between date_sub(now(),interval 6 month) and now();  

12.查询当前这周的数据

select * from byzp_personinfo WHERE YEARWEEK(date_format(birthday,'%Y-%m-%d')) = YEARWEEK(now());  

13.查询上周的数据

select * from byzp_personinfo WHERE YEARWEEK(date_format(birthday,'%Y-%m-%d')) = YEARWEEK(now())-1;  

14.查询上个月的数据

select * from byzp_personinfo where date_format(birthday,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m'); 

15.查询当前月份的数据

select * from byzp_personinfo where DATE_FORMAT(birthday,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m');
select * from byzp_personinfo where date_format(birthday,'%Y-%m')=date_format(now(),'%Y-%m'); 

16.查询指定时间段的数据

select * from byzp_personinfo where birthday < '2018-4-1 00:00:00'  or birthday >'2018-10-31 00:00:00';   
select * from byzp_personinfo where birthday >= '2018-6-1 00:00:00' and birthday < '2019-1-1 00:00:00'; 

17.查询月份数据

语法:
	select * from table_name where month(date)='2';
示例:
	查询表byzp_personinfo中2月份生日的人
	select * from byzp_personinfo where month(birthday) = '2';

18.查询年份数据

语法:
	select * from table_name where year(date)=年份;
示例:
	查询表byzp_personinfo中2019年的数据
	select * from byzp_personinfo where year(birthday) = '2019';
	
注:year,month,dayofyear是mysql的函数,分别是取得年、月、当前时间在本年是第几天的3个函数。
	mysql数据库查询某一年内各月份数据,按月份分组
	select month(birthday) as month,sum(id) from byzp_personinfo where year(birthday) = 2018 group by month (birthday);

转载自 https://www.cnblogs.com/apollo1616/p/10404460.html