I have two tables which are linked with foreign key relationship. Both tables have Created Date columns. How can I find all matching rows where time difference is 5 minutes plus or minus.
2 Answers
You'll need to use the timestampdiff function and the abs function, something like this:
select * from table1 a, table2 b where a.ID=b.FOREIGNID and ABS(TIMESTAMPDIFF(MINUTE,a.CREATEDDATE,b.CREATEDDATE)) > 5
Documentation for both functions:
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_abs
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
Comments
That should work. Or do you need it for both sides?
SELECT *
FROM tableA a
JOIN tableB b
ON a.keyInfo = b.keyInfo
AND b.created BETWEEN a.created - 5/(24*60) AND a.created + 5/(24*60)
1 Comment
Stefan
Then use Jerry´s answer (probably it has to be <= 5 at the end)