TIMEX
TIMEX

Reputation: 272264

How do I do this query in MySQL? (datetime)

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 ...

Upvotes: 0

Views: 107

Answers (2)

Pavunkumar
Pavunkumar

Reputation: 5345

Try this ...

SELECT * from table_name  
  where (extract ( epoch from current_datetime ) 
         - extract (epoch from that_datetime))  < 2500

Upvotes: 1

vava
vava

Reputation: 25401

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.

Upvotes: 1

Related Questions