简体   繁体   中英

How do I make a scheduled MySql query to pick data every top of the hour?

I have one table that has timestamped(DATETIME) values.

Table MyLog:

+----------+---------------------+--------+
+
| logNo    | timestamp           | temp   |
|
+----------+---------------------+--------+
+
|        1 | 2016-04-24 15:57:56 |   29.6 |
|
|        2 | 2016-04-24 16:59:24 |   29.7 |
|
|        3 | 2016-04-24 18:01:52 |   29.6 |
|
|        4 | 2016-04-24 18:59:20 |   29.6 |
+----------+---------------------+--------+

I have another table (HourLog) that pulls the 'temp' value from 'MyLog' every top of the hour(HH:00:00) with the value that has a 'timestamp' value closest to the 'timestamp' value in HourLog (because the timestamp values can't always be exactly HH:00:00).

HourLog:

+----------+---------------------+--------+
+
|logNo      | timestamp          | temp   |
|
+----------+---------------------+--------+
+
|        1 | 2016-04-24 00:00:00 |   29.6 |
|
|        2 | 2016-04-24 01:00:00 |   29.7 |
|
|        3 | 2016-04-24 02:00:00 |   29.6 |
|
|        4 | 2016-04-24 03:00:00 |   29.6 |
|
|        5 | 2016-04-24 04:00:00 |   29.7 |
|
|        6 | 2016-04-24 05:00:00 |   29.8 |
|
|        7 | 2016-04-24 06:00:00 |   30.2 |
|
|        8 | 2016-04-24 07:00:00 |   30.1 |
|
|        9 | 2016-04-24 08:00:00 |   30.1 |
|
|       10 | 2016-04-24 09:00:00 |   30.1 |
|
|       11 | 2016-04-24 10:00:00 |   30.0 |
|
|       12 | 2016-04-24 11:00:00 |   30.1 |
|
|       13 | 2016-04-24 12:00:00 |   30.1 |
|
|       14 | 2016-04-24 13:00:00 |   29.9 |
|
|       15 | 2016-04-24 14:00:00 |   29.8 |
|
|       16 | 2016-04-24 15:00:00 |   29.6 |
|
|       17 | 2016-04-24 16:00:00 |   29.6 |
|
|       18 | 2016-04-24 17:00:00 |   29.7 |
|
|       19 | 2016-04-24 18:00:00 |   29.6 |
|
|       20 | 2016-04-24 19:00:00 |   29.6 |
+----------+---------------------+--------+

The query I want to write should be able to look at the timestamp value in MyLog, get the value closest to the current hour and plug the 'temp' value into HourLog with the (HH:00:00) timestamp.

An example: MyLog

+----------+---------------------+--------+
+
| logNo    | timestamp           | temp   |
|
+----------+---------------------+--------+
+
|      101 | 2016-04-24 17:57:56 |   29.6 |
|
|      102 | 2016-04-24 17:59:24 |   29.7 |
|
|      103 | 2016-04-24 18:01:52 |   29.6 |
|
|      104 | 2016-04-24 18:04:20 |   29.6 |
+----------+---------------------+--------+

Query selects logNo 102 because its timestamp value is the closest to the top of the hour (18:00:00) and plugs the 'temp' value into HourLog logNo 74

HourLog:

+----------+---------------------+--------+
+
| logNo    | timestamp           | temp   |
|
+----------+---------------------+--------+
+
|       71 | 2016-04-24 15:00:00 |   28.6 |
|
|       72 | 2016-04-24 16:00:00 |   28.7 |
|
|       73 | 2016-04-24 17:00:00 |   30.1 |
|
|       74 | 2016-04-24 18:00:00 |   29.6 |
+----------+---------------------+--------+

How can I write such a scheduled query in MySQL?

Bear with me, but try creating a SQL file like this call it query.sql :

SET @n = NOW();

INSERT INTO HourLog

SELECT 0, @n, X.temp (
    SELECT 
        TIMESTAMPDIFF(SECOND, timestamp, @n) diff, 
        temp
    FROM MyLog
    ORDER BY diff ASC
    LIMIT 1
) X;

Then create a shell script that calls that file ( script.sh ):

mysql --login-path=[your login path] [your database] < /path/to/query.sql

Then you should be able to call a cron job like this:

0 * * * * /path/to/script.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM