Answer the question
In order to leave comments, you need to log in
How to properly write a scheduler with a counter in MySQL?
Hello comrades! Please help me figure it out. MySQL database
has a table called "TABLE_MAIN" . I need to populate this table with new data every hour from other tables ( "TABLE_A" and "TABLE_B" ) based on the underlying SQL query. In the SQL query itself, depending on the time, you need to update the passed value for A.TIME_KEY and B.MONTH_KEY . For example, an hour has passed, run the SQL query, but for A.TIME_KEY pass the value "2018-01-05 01:00:00" . The value for B.MONTH_KEY will remain the same. Value in B.MONTH_KEYwill only change when the month changes. For example, for such a value 2018-02-01 . How to write a scheduler with such a counter in MySQL?!
INSERT INTO TABLE_MAIN (
ID,
TIME_KEY,
STATUS,
OBJECT_NAME,
OBJECT_DESCRIPTION
) VALUES (
SELECT
A.ID
A.TIME_KEY
A.STATUS
B.OBJECT_NAME
B.OBJECT_DESCRIPTION
FROM
TABLE_A AS A
INNER JOIN
TABLE_B AS B
ON
A.ID = B.ID_OBJECT
WHERE
A.TIME_KEY="2018-01-05 00:00:00"
AND
B.MONTH_KEY="2018-01-01"
);
Answer the question
In order to leave comments, you need to log in
Use the current date and rounding.
dateadd(month ,datediff(month, 0, GETDATE( ) ), 0) this will allow you to round up to the day of the month.
dateadd(hour ,datediff(hour, 0, GETDATE( ) ), 0) which will give the start of the current hour.
Instead of zero in all places, you can use any date less than the current one (but the same everywhere).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question