Background: During the development process, we often need to query data based on time as a judgment condition, such as: the current month, the current day, the current hour, the next few days... 1. Current month We only need to use a MySQL MONTH(date) function to achieve this. (Note the year)
2. Within 30 days The reason why I put "within 30 days" after the current month is because I often encounter situations where these two requirements are converted into each other. "Within 30 days" can also be called "within a month." In this case we need to use the DATEDIFF(expr1,expr2) function.
3. On the day The TO_DAYS(date) function needs to be used for the current day.
4. Current hour In this case, the HOUR(date) and CURDATE() functions need to be used together.
5. Within x days This can be achieved using the DATE_SUB(date,INTERVAL expr unit) function.
There are many values for the unit behind it, as shown in the following table:
6. Data statistics within a few days We often encounter this requirement, which is to count the amount of data every day within 7 days. In this case, we need to consider the case where there is no data push, that is, we must get it even if it is 0. My thoughts are as follows: SELECT DATE_SUB( CURDATE(), INTERVAL ms day ) AS orderDate FROM ( SELECT 0 as s UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 ) Use the above SQL query results as a temporary table to match the data table to count the number of data within a certain period of time -- Example: Query the number of data in each month within 12 months SELECT COUNT(t.created_at),res.date FROM (SELECT DATE_FORMAT(DATE_SUB( CURDATE(), INTERVAL ms MONTH ),'%Y-%m') AS date FROM ( SELECT 0 as s UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 ) m)res left join table t on res.date = DATE_FORMAT(t.created_at,'%Y-%m') GROUP BY date The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Why can't I see the access interface for Docker Tomcat?
>>: Detailed explanation of where the images pulled by docker are stored
As shown below: update table1 as z left join tabl...
Socat needs to be installed before installing rab...
This article describes the usage of MySQL stored ...
SMIL adds support for timing and media synchroniz...
First check the /etc/group file: [root@localhost ...
Under the requirements of today's responsive ...
Table of contents How to represent the current ti...
Monitoring method in Vue watch Notice Name: You s...
For reference only for Python developers using Ub...
The operating system for the following content is...
Table of contents 1. Scenario 2. Basic functions ...
The Core Asset Management Project requires el-tra...
Error message: ERROR 2002: Can't connect to l...
Preface I feel like my mind is empty lately, as I...
Preface: Integer is one of the most commonly used...