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
First, let me explain that what we want to do is ...
Let’s look at an example first Copy code The code ...
Process 1: with return value: drop procedure if e...
Server Status Analysis View Linux server CPU deta...
1. When to execute setUp We all know that vue3 ca...
Table of contents 1. The relationship between red...
1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...
Syntax: <marquee> …</marquee> Using th...
Mainly use the preserve-3d and perspective proper...
Yesterday I installed CentOS7 under VMware. I wan...
Table of contents Select Structure Loop Structure...
Table of contents 1. Introduction 2. GitHub 3. Ba...
There is no problem with the Dockerfile configura...
Operating system: Window10 MySQL version: 8.0.13-...
Effect: Ideas: Use the input type attribute to di...