user3325669
user3325669

Reputation: 67

SQL Check if a value is close to any value in a different table

Having a problem with a sql question. Here are the two tables:

ID   |   Timestamp
1    |   143
2    |   429

Timestamps_master
150
322
539

The question I want to ask is: how many IDs in the first table have a timestamp that's within 10 seconds of a timestamp in timestamps_master. So in the above set-up, it should be 1.

Outside sql, I'd just do some loops, but can't figure out what the sql equivalent would be.

Thanks for any ideas!

Upvotes: 0

Views: 140

Answers (2)

SamN
SamN

Reputation: 21

SELECT t1.id, t1.timestamp 
FROM t1, t2 
WHERE t1.timestamp BETWEEN t2.time-10 and t2.time+10

Upvotes: 0

Clar Cleetus
Clar Cleetus

Reputation: 275

select Id from tablename where(diff(Timestamps_master,Timestamp)<=10)or (diff(Timestamps_master,Timestamp)>=-10);

Upvotes: 3

Related Questions