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 TableSELECT @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. 2. 3. 4. 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 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:
|
<<: How to Clear Disk Space on CentOS 6 or CentOS 7
>>: JS Canvas interface and animation effects
When applying docker containers, we often mount t...
This article shares the MySQL precompilation func...
Scenario: When starting tomcat in docker (version...
Tabs: Category + Description Tag bar: Category =&...
JavaScript - Principles Series In daily developme...
Compatible with new CSS3 properties In CSS3, we c...
There are significant differences between centos7...
In the past, creating a printer-friendly version ...
Preface MySQL database lock is an important means...
Sometimes the page is very long and needs an arro...
The domestic market still has a certain demand fo...
Recently, when using kazam in Ubuntu 20.04 for re...
Use the find command to find files larger than a ...
Table of contents introduction 1. Case Overview 2...
Readonly and Disabled both prevent users from chan...