当前位置:网站首页>MySQL queries the data of the current day, this week, this month and last month
MySQL queries the data of the current day, this week, this month and last month
2022-07-19 05:36:00 【Lun Zhi】
SELECT * FROM Table name WHERE to_days( Time field name ) = to_days(now());
SELECT * FROM Table name WHERE TO_DAYS(NOW()) - TO_DAYS( Time field name ) <= 1 AND TO_DAYS(NOW()) - TO_DAYS( Time field name ) > 0;
SELECT * FROM Table name WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date( Time field name );
SELECT * FROM Table name WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date( Time field name );
SELECT * FROM Table name WHERE QUARTER( Time field name ) = QUARTER(now());
SELECT * FROM Table name WHERE date_format( Time field name ,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH), '%Y-%m'); SELECT * FROM Table name WHERE DATE_FORMAT( Time field name ,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; SELECT * FROM Table name WHERE WEEKOFYEAR(FROM_UNIXTIME( Time field name ,'%y-%m-%d')) = WEEKOFYEAR(now()); SELECT * FROM Table name WHERE MONTH(FROM_UNIXTIME( Time field name ,'%y-%m-%d')) = MONTH(now()); SELECT * FROM Table name WHERE YEAR(FROM_UNIXTIME( Time field name ,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME( Time field name ,'%y-%m-%d')) = MONTH(now()); SELECT * FROM Table name WHERE Time field name between Last day of last month and The first day of next month ;
DATE_FORMAT(date,format)
date Parameter is a valid date .format Specified date / Time output format .
The available formats are :
| Format | describe |
|---|---|
| %a | Abbreviated week name |
| %b | Abbreviated month name |
| %c | month , The number |
| %D | Days of the month with English prefix |
| %d | Day of the month , The number (00-31) |
| %e | Day of the month , The number (0-31) |
| %f | Microsecond |
| %H | Hours (00-23) |
| %h | Hours (01-12) |
| %I | Hours (01-12) |
| %i | minute , The number (00-59) |
| %j | Days of (001-366) |
| %k | Hours (0-23) |
| %l | Hours (1-12) |
| %M | Month name |
| %m | month , The number (00-12) |
| %p | AM or PM |
| %r | Time ,12- Hours (hh:mm:ss AM or PM) |
| %S | second (00-59) |
| %s | second (00-59) |
| %T | Time , 24- Hours (hh:mm:ss) |
| %U | Zhou (00-53) Sunday is the first day of the week |
| %u | Zhou (00-53) Monday is the first day of the week |
| %V | Zhou (01-53) Sunday is the first day of the week , And %X Use |
| %v | Zhou (01-53) Monday is the first day of the week , And %x Use |
| %W | Week name |
| %w | Days of the week (0= Sunday , 6= Saturday ) |
| %X | year , Sunday is the first day of the week ,4 position , And %V Use |
| %x | year , Monday is the first day of the week ,4 position , And %v Use |
| %Y | year ,4 position |
| %y | year ,2 position |
example
The following script uses DATE_FORMAT() Function to display different formats . We use NOW() To get the current date / Time :
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p') DATE_FORMAT(NOW(),'%m-%d-%Y') DATE_FORMAT(NOW(),'%d %b %y') DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
边栏推荐
猜你喜欢

电商用户行为实时分析系统(Flink1.10.1)

线程池如何监控,才能帮助开发者快速定位线上错误?

10问10答:你真的了解线程池吗?

1. Dongsoft Cross - Border E - commerce Data Warehouse Requirement specification document

4. Neusoft cross border e-commerce data warehouse project - user behavior data acquisition channel construction of data acquisition channel construction (2022.6.1-2022.6.4)

Ambari cluster expansion node + expansion service operation

2.东软跨境电商数仓项目技术选型

柠檬班软件测试培训可靠吗 这个从培训班逆袭成功的案例告诉你

1.东软跨境电商数仓需求规格说明文档

C language & bit field
随机推荐
MySQL installation and configuration tutorial (super detailed)
MySQL 查询当天、本周,本月、上一个月的数据
Scala primary practice - statistics of mobile phone traffic consumption (1)
MySQL -- storage and cursor
What is the employment prospect of software testing? There is a large demand for talents and strong job stability
线程池如何监控,才能帮助开发者快速定位线上错误?
使用Flink SQL传输市场数据1:传输VWAP
1 SparkSQL概述
关于线程池中终止任务
C language dynamic memory management
用Flink SQL流化市场数据2:盘中风险价值
Is the software testing training of lemon class reliable? This successful case of counter attack from the training class tells you
ambari 2.7.5集成安装hue 4.6
ETL tool -- kettle realizes simple data migration
操作系统常见面试题
解决idea新建module 提示module xxxx does exitst
递归的应用
Operation of C language files
ETL工具——kettle实现简单的数据迁移
C language & bit field