Display rows from MySQL where a datetime is within the next hour

前端 未结 2 542
无人共我
无人共我 2021-01-12 07:24

I always have trouble with complicated SQL queries.

This is what I have

$query = \'
            SELECT id,
                   name, 
                         


        
2条回答
  •  悲&欢浪女
    2021-01-12 07:55

    I'm going to postulate that you're looking at a group of records that contain a range of DATETIME values, so you probably want something more 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
    

    Otherwise, your query is looking for records with a date_time of exactly "now + 1 hour". I'm assuming all your dates aren't specific to that particular second. ;)

    To clarify a bit, DATE_ADD() and DATE_SUB() return exact timestamps, so your query above roughly translates to something like SELECT ... WHERE date_time = '2010-04-14 23:10:05' ORDER BY ..., which I don't think is what you want.

提交回复
热议问题