Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using the difference between two dates in a WHERE clause

Tags:

sql

mysql

I'm trying to filter a SELECT query between NOW() and NOW - interval 10 minute(?), but i can't seem to get this to work, and it's given me a few questions on the topic.

I've looked through some documentation online, and alot of questions on stackoverflow but non of the solutions give me what i need. Looking at the TIMEDIFF and TIMESTAMPDIFF documentation, i only see it used like this;

SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00'); 

However i don't want to just select the time difference, i want to use it in a query as a WHERE clause, something like;

SELECT * FROM tableName WHERE (the time difference betweeen NOW() and the stored timestamp is less than x minutes);

Is there a particular data type i need to set my column to? How can i use the TIMEDIFF / TIMESTAMPDIFF correctly, and if these are not the correct methods i should be using, what is?

like image 740
Kestami Avatar asked Jul 04 '26 00:07

Kestami


1 Answers

SELECT * FROM tableName WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) < 10
like image 167
NewInTheBusiness Avatar answered Jul 07 '26 19:07

NewInTheBusiness



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!