Display rows from MySQL where datetime is in the next hour

I always have problems with complex SQL queries.

This is what I have

$query = '
            SELECT id,
                   name, 
                   info, 
                   date_time
            FROM acms_events
                WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
                    AND active = 1
            ORDER BY date_time ASC
            LIMIT 6
        ';

I want to get up to 6 rows that will be available within an hour. Is my request wrong? It does not seem to receive the events that will be expected over the next hour when I try it.

What is the correct syntax for this?

+5
source share
2 answers

I am going to postulate that you are looking at a group of records that contain a range of DATETIME values, so you probably want something like this:

SELECT id,
       name, 
       info, 
       date_time
FROM acms_events
    WHERE date_time < DATE_ADD(NOW(), INTERVAL 1 HOUR)
        AND date_time >= NOW()
        AND active = 1
ORDER BY date_time ASC
LIMIT 6

_ " + 1 ". , .;)

, DATE_ADD() DATE_SUB() , - SELECT ... WHERE date_time = '2010-04-14 23:10:05' ORDER BY ..., , , , .

+8
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)

, date_time , .

WHERE TIMEDIFF(date_time, NOW()) < '01:00:00'
AND date_time > NOW()
0

All Articles