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
In the vue scaffolding, we can see that in the ne...
This article shares the specific process of js ob...
The operating environment of this tutorial: Windo...
What is Docker-Compose The Compose project origin...
This article shares the specific code of Vue to a...
Preface The author has always felt that it would ...
This article will introduce how to query data in ...
Table of contents 1. Detailed syntax of entires()...
This article mainly introduces the effect of div ...
This article uses examples to illustrate the func...
After installing the latest version 8.0.11 of mys...
Table of contents 1. Connection Management 2. Imp...
1. HTML part <Col span="2">Upload...
This article uses the crontab command in the Linu...
Locks in MySQL Locks are a means to resolve resou...