MySQL daily statistics report fills in 0 if there is no data on that day

MySQL daily statistics report fills in 0 if there is no data on that day

1. Problem reproduction:

Count the total number of each day by day. If there are days with no data, then the group by return will ignore those days. How to fill them with 0? As shown in the figure below, the data for the seven days from 10-3 to 10-10 are counted, of which only the 8th and 10th have data. In this way, only 2 data are returned, which does not meet the statistical requirements of the report. Expect no value to be filled with 0

2. Change your mindset:

We use a set of consecutive days as the left table and then left join the data to be queried and finally group by: consecutive days table t1 left join business data t2 group by t1.day, as shown below:

SELECT
 t1.`day`,
 COUNT(t2.user_id) payment_num
FROM
 (SELECT
 @cdate := DATE_ADD(@cdate, INTERVAL - 1 DAY) DAY
 FROM
 (SELECT
  @cdate := DATE_ADD('20171219', INTERVAL + 1 DAY)
 FROM
  order) t0
 LIMIT 7) t1
 LEFT JOIN
 (SELECT
  DATE(a.create_time) DAY,
  a.user_id
 FROM
  pay_payment a
  JOIN dealer_store b
  ON a.order_no = b.order_no
 WHERE DATE(create_time) <= '20171219'
  AND DATE(create_time) > DATE_SUB('20171219', INTERVAL 7 DAY)
  ) t2
 ON t2.day = t1.day
GROUP BY t1.`day`;

2.1 Consecutive Days Table

SELECT
 @cdate := DATE_ADD(@cdate, INTERVAL - 1 DAY) DAY
 FROM
 (SELECT
  @cdate := DATE_ADD('20171219', INTERVAL + 1 DAY)
 FROM
  order) t0
 LIMIT 7

The execution results are as follows:

SQL analysis:

1. @cdate := 是定義名為cdate的變量并賦值(select 后面必須用:=)

2. .@cdate := DATE_ADD('20171219', INTERVAL + 1 DAY) adds one day to the passed date '20171219'

3. SELECT @cdate := DATE_ADD('20171219', INTERVAL + 1 DAY) FROM `order` and find a table with more than 10 records. The execution results are as follows:

4. .@cdate := DATE_ADD(@cdate, INTERVAL - 1 DAY) DAY decrements the number of days in the defined cdate variable by 1 (self-decrement)

5. LIMIT 7 Limit the number of records. That’s it. We have obtained the records 7 days before the specified date.

2.2 Left join and then group

left join group by t1.day

That is, according to the business data associated with the left table, the data is grouped according to the date of the left table, that is, it is divided into the specified 7 days of data. If there is a record, the number of entries is counted, and if there is no record, it is 0.

Final execution result:

Summarize

The above is the implementation code that I introduced to you for filling 0 in MySQL according to the daily statistical report if there is no data on that day. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • MySQL statistical query implementation code
  • Problems with using the IF function (case) to calculate conditional statistics for the SUM field in MYSQL
  • How to use MySQL to query the number of identical values ​​in a column
  • Example of using MySQL to count the number of different values ​​in a column
  • A single select statement implements MySQL query statistics
  • MYSQL implementation method of grouping statistics every 10 minutes
  • Basic usage tutorials for several data statistics queries in MySQL
  • Summary of MySQL time statistics methods
  • mysql obtains statistical data within a specified time period
  • Analysis of traps in using the MySQL statistical function GROUP_CONCAT
  • Example analysis of the principle and solution of MySQL sliding order problem

<<:  How to Clear Disk Space on CentOS 6 or CentOS 7

>>:  JS Canvas interface and animation effects

Recommend

Detailed explanation of MySQL precompilation function

This article shares the MySQL precompilation func...

404 error occurs when accessing the homepage of tomcat started in Docker mode

Scenario: When starting tomcat in docker (version...

Several implementation methods of the tab bar (recommended)

Tabs: Category + Description Tag bar: Category =&...

Execution context and execution stack example explanation in JavaScript

JavaScript - Principles Series In daily developme...

Detailed explanation of commonly used CSS styles (layout)

Compatible with new CSS3 properties In CSS3, we c...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Convert XHTML CSS pages to printer pages

In the past, creating a printer-friendly version ...

Understanding MySQL Locking Based on Update SQL Statements

Preface MySQL database lock is an important means...

Ubuntu 20.04 turns on hidden recording noise reduction function (recommended)

Recently, when using kazam in Ubuntu 20.04 for re...

Docker configures the storage location of local images and containers

Use the find command to find files larger than a ...

Implementation example of Nginx+Tomcat load balancing cluster

Table of contents introduction 1. Case Overview 2...

A detailed explanation of the subtle differences between Readonly and Disabled

Readonly and Disabled both prevent users from chan...