0

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.

1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Then use Jerry´s answer (probably it has to be <= 5 at the end)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.