I
I
ILoveYAnny2016-09-03 17:36:46
MySQL
ILoveYAnny, 2016-09-03 17:36:46

How to make SQL (MySQL) that add a record if 1 hour has passed since the last one?

Hello, please tell me a PHP command that will check if 1 hour has passed, and if so, add a
MySQL database record
. I had such a command for PostgreSQL, but it doesn’t fit MySQL. It returned false if the gap didn't pass

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Omelchenko, 2016-09-03
@pOmelchenko

if/else

M
Melkij, 2016-09-03
@melkij

insert into tablename (datetimefield /*other fields*/) 
    select now(), /*other fields*/ 
    from (select 1) anyonerow 
    where not exists(
        select 1 from tablename where datetimefield >= now() - interval 1 hour
    )

The result of whether a record was added is to look in affected_rows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question