title: Join tables on nearest date in the past, in MySQL
body: <p>I have a sqlite query that I'm trying to write. I have two tables:</p>
  
  <p>TableA (sales):
  id
  sales
  date</p>
  
  <p>TableB (goals):
  id
  goal
  date</p>
  
  <p>I'm selecting from TableA like this:
  SELECT id,sales,date FROM TableA</p>
  
  <p>Now to the "tricky" part. I need to join TableB to the query because I need the goal field in TableB <strong>for each row</strong> in TableA. <strong>TableB only contains goals for some dates</strong>, while <strong>TableA contains all dates</strong>. So I can't just use TableA.date = TableB.date</p>
  
  <p>Instead, for each row in TableA I need to take the goal from TableB on the date nearest in the past to the date in TableA. Hope I was able to explain what I needed. Can't figure out how to do it..</p>