views:

23

answers:

2

I have a timestamp column that auto updates on insert/update.

I want to get the rows that have been updated within the last 10 minutes.

SELECT
     *
FROM
     status
WHERE
     code='myCode'
AND
     'stamp_updated' 
     BETWEEN
     NOW()
     AND
     DATE_ADD(NOW() , INTERVAL - 10 MINUTE)
ORDER BY 
     stamp_updated DESC
LIMIT 1
+2  A: 

Use:

  SELECT *
    FROM status
   WHERE code = 'myCode'
     AND `stamp_updated` BETWEEN DATE_SUB(NOW() , INTERVAL 10 MINUTE)
                           AND NOW()
ORDER BY stamp_updated DESC
   LIMIT 1

Order in the BETWEEN operator matters - you had it backwards.

OMG Ponies
for some reason I keep getting empty result sets even after I update the rows to have the current time.. could you review and see if maybe somethings missing?
krio
@krio: These rows you're updating - is their `code` value set to "myCode"?
OMG Ponies
@OMG Ponies: yes, I'm actually removing that and just using (for testing): SELECT * FROM status WHERE 'stamp_updated' BETWEEN DATE_SUB(NOW() , INTERVAL 50 MINUTE) AND NOW()
krio
@krio: Ok, then is the `stamp_updated` defined as a DATETIME data type? Mind posting in your question what you get when you run: `DESC status`?
OMG Ponies
The auto update timestamp wasn't correctly firing because the data was the same so no updates were actually occurring. Your code worked great, thank you!
krio
+2  A: 
 ... 'stamp_updated' BETWEEN NOW() - INTERVAL 10 MINUTE AND NOW()  ...
a1ex07
+1: I was not even 20 seconds faster that you
OMG Ponies