Demand backgroundA statistical interface, the front end needs to return two arrays, one is the hour count from 0 to 23, and the other is the statistical number corresponding to each hour. The idea is to directly use group by to query the table to be counted. When the statistical number of a certain hour is 0, there will be no grouping for that hour. After thinking about it, I need to create an auxiliary table with only one column for hours, and then insert 0-23 for a total of 24 hours CREATE TABLE hours_list ( hour int NOT NULL PRIMARY KEY ) Check the hour table first, then connect the table you need to check, and fill in the hours without statistics with 0. Here, because we need to query multiple tables, create_time is within each hour interval, and SOURCE_ID is equal to the statistical sum of the query conditions, so UNION ALL multiple tables. SELECT t.HOUR, sum(t.HOUR_COUNT) hourCount FROM (SELECT hs. HOUR AS HOUR, COUNT(cs.RECORD_ID) AS HOUR_COUNT FROM cbc_hours_list hs LEFT JOIN cbc_source_0002 cs ON HOUR (cs.create_time) = hs.HOUR AND cs.create_time > #{startTime} AND cs.create_time <= #{endTime} <#if sourceId?exists && sourceId !=''> AND SOURCE_ID = #{sourceId} </#if> GROUP BY hs. HOUR UNION ALL SELECT hs.HOUR AS HOUR, COUNT(cs.RECORD_ID) AS HOUR_COUNT FROM cbc_hours_list hs LEFT JOIN cbc_source_hs cs ON HOUR (cs.create_time) = hs.HOUR AND cs.create_time > #{startTime} AND cs.create_time <= #{endTime} <#if sourceId?exists && sourceId !=''> AND SOURCE_ID = #{sourceId} </#if> GROUP BY hs. HOUR UNION ALL SELECT hs.HOUR AS HOUR, COUNT(cs.RECORD_ID) AS HOUR_COUNT FROM cbc_hours_list hs LEFT JOIN cbc_source_kfyj cs ON HOUR (cs.create_time) = hs.HOUR AND cs.create_time > #{startTime} AND cs.create_time <= #{endTime} <#if sourceId?exists && sourceId !=''> AND SOURCE_ID = #{sourceId} </#if> GROUP BY hs. HOUR UNION ALL SELECT hs.HOUR AS HOUR, COUNT(cs.RECORD_ID) AS HOUR_COUNT FROM cbc_hours_list hs LEFT JOIN cbc_source_his_0002 cs ON HOUR (cs.create_time) = hs.HOUR AND cs.create_time > #{startTime} AND cs.create_time <= #{endTime} <#if sourceId?exists && sourceId !=''> AND SOURCE_ID = #{sourceId} </#if> GROUP BY hs. HOUR UNION ALL SELECT hs.HOUR AS HOUR, COUNT(cs.RECORD_ID) AS HOUR_COUNT FROM cbc_hours_list hs LEFT JOIN cbc_source_his_hs cs ON HOUR (cs.create_time) = hs.HOUR AND cs.create_time > #{startTime} AND cs.create_time <= #{endTime} <#if sourceId?exists && sourceId !=''> AND SOURCE_ID = #{sourceId} </#if> GROUP BY hs. HOUR UNION ALL SELECT hs.HOUR AS HOUR, COUNT(cs.RECORD_ID) AS HOUR_COUNT FROM cbc_hours_list hs LEFT JOIN cbc_source_his_kfyj cs ON HOUR (cs.create_time) = hs.HOUR AND cs.create_time > #{startTime} AND cs.create_time <= #{endTime} <#if sourceId?exists && sourceId !=''> AND SOURCE_ID = #{sourceId} </#if> GROUP BY hs. HOUR) t GROUP BY t.hour EffectHours with a statistical count of 0 can also be found. This is the end of this article about querying MySQL data by the hour and filling in zeros for missing data. For more information about querying MySQL data by the hour, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Enable sshd operation in docker
>>: Detailed explanation of Vue mixin usage and option merging
I mainly introduce how to develop a lucky wheel g...
I have read an article written by the Yahoo team ...
The main symptom of the conflict is that the FLASH...
Recently, I have been learning to use nginx to pl...
I slept late yesterday and was awake the whole da...
As shown below: CSS CodeCopy content to clipboard...
This article uses a specific example to introduce...
First, let me introduce how to install PHP on Cen...
We often encounter this problem: how to use CSS t...
Mysql5.7.19 version is a new version launched thi...
Click here to return to the 123WORDPRESS.COM HTML ...
1. It is best to add a sentence like this before t...
This article shares the specific code for WeChat ...
Preface WeChat Mini Programs provide new open cap...
Table of contents 1. Install vue-video-player 2. ...