views:

42

answers:

2

Suppose I have a datetime column in MySQL. How do I select all that have a datetime within 2500 seconds of the current datetime?

SELECT ALL where current_datetime - that_datetime < 2500 ...
+1  A: 
SELECT * FROM Table WHERE that_datetime > NOW() - INTERVAL 2500 SECOND

You want to do all the function calls and operation on constants as MySQL won't use indexes for fields used in any kind of expressions.

vava
INTERVAL 2500 SECOND goes in the query too?
TIMEX
Yes, it is a way to do time arithmetic in MySQL. `SECOND` can be a `MINUTE`, `HOUR`, `DAY`, `MONTH`, `YEAR`, whatever you choose it to be.
vava
+1  A: 

Try this ...

SELECT * from table_name  
  where (extract ( epoch from current_datetime ) 
         - extract (epoch from that_datetime))  < 2500
pavun_cool
That is a really bad way to do queries. Indexes goes out of the window.
vava