title: Filter objects within two seconds of one another using SQLAlchemy
body: <p>I have two tables with a column 'date'. One holds (name, date) and the other holds (date, p1, p2). Given a name, I want to use the date in table 1 to query p1 and p2 from table two; the match should happen if date in table one is within two seconds of date in table two.</p>
  
  <p>How can you accomplish this using SQLAlchemy?</p>
  
  <p>I've tried (unsuccessfully) to use the <code>between</code> operator and with a clause like:</p>
  
  <pre><code>td = datetime.timedelta(seconds=2)
  q = session.query(table1, table2).filter(table1.name=='my_name').\
      filter(between(table1.date, table2.date - td, table2.date + td))
  </code></pre>
  
  <p>Any thoughts?</p>
  
  <p><strong>Edit:</strong>
  I've managed to solve the problem using the following approach:</p>
  
  <pre><code>from sqlalchemy.sql import between
  import datetime
  # [all other relevant imports]
  
  td = datetime.timedelta(seconds=2)
  t1_entry = session.query(table_1).filter(table_1.name == 'the_name').first()
  if t1_entry is not None:
   tmin = t1_entry.date - td
   tmax = t1_entry.date + td
   t2_entry = session.query(table_2).filter(between(table_2.date, tmin, tmax)).first()
   return (t1_entry, t2_entry)
  return None
  </code></pre>
  
  <p>So the comparison can be done, but I'm not sure the approach is efficient.</p>