Mysql gets the last 7 days (date), the last 6 months and the last year (month)

Mysql gets the last 7 days (date), the last 6 months and the last year (month)

Overview:

​ In daily java development, we may encounter the situation of making reports based on time statistics. Although the function of reporting can be realized through java language, in order to optimize the code, we try to implement this operation in the database as much as possible.

​ The main knowledge points realized in this note are:

  • Retrieve dates within the last 7 days, months within the last 6 months, and months within the last year
  • The joint data table realizes the report statistics, (the date or month without data is automatically filled with 0)

MySQL implements retrieval of the date of the past 7 days, the month of the past 6 months and the month of the past year

1. Retrieve the date data of the last 7 days

SELECT
@s :=@s + 1 as `index`,
DATE(DATE_SUB(CURRENT_DATE, INTERVAL @s DAY)) AS `date`
FROM 
mysql.help_topic,
(SELECT @s := -1) temp
WHERE 
@s < 6
ORDER BY `date`

The date of the note is 2021.3.23, and the search results are as follows:

insert image description here

If you want to change the number of days to be retrieved, you can do so by modifying the where condition. The above code only shows the operation of searching for the past week.

(The above code can be directly copied and used without creating a new table, and the data table that comes with the system is used)

2. Retrieve the months of the past 6 months and the past year

SELECT 
@s :=@s + 1 as `index`,
DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL @s MONTH),'%Y-%m') AS `mon`
FROM 
mysql.help_topic,
(SELECT @s := -1) temp
WHERE 
@s < 5
ORDER BY mon

The month of writing this note is 2021.3, and the retrieval results are as follows:

When in the where condition, @s < 5, the search result is the month information of the past 6 months, and the results are as follows:

insert image description here

When in the where condition, @s < 11, the search result is the month information of the past year, and the results are as follows:

insert image description here

You can modify the where condition according to the number of months you want to get the month information that meets the requirements.

The joint data table realizes the report statistics, (the date or month without data is automatically filled with 0)

Go directly to the code:

SELECT t1.date,IFNULL(t2.SUM , 0) as sum
from
(SELECT
@s :=@s + 1 as `index`,
DATE(DATE_SUB(CURRENT_DATE, INTERVAL @s DAY)) AS `date`
FROM 
mysql.help_topic,
(SELECT @s := -1) temp
WHERE 
@s < 6
ORDER BY `date`) as `t1`
left join 
(SELECT 
DATE_FORMAT(create_date,'%Y-%m-%d') td,
COUNT(id) SUM
from test
where 
TO_DAYS(NOW())-TO_DAYS(create_date) <= 6
GROUP BY td ASC ) as `t2`
on t1.date = t2.td

The second table in the left join above is a table created by myself, and the data is grouped according to time (the data is not displayed when the data is 0), and when the data is 0 after the joint table query, it will also be automatically filled with 0

The achieved results are as follows:

insert image description here

The "IFNULL" operation in the first line of the code is the key to automatically fill in 0 when there is no data.

Other operations are joint queries between two tables through the operation of left join on

Guess you like

Origin blog.csdn.net/xiri_/article/details/115110704
Recommended