Join tables on nearest date in the past, in MySQL

前端 未结 3 757
别那么骄傲
别那么骄傲 2020-12-19 15:56

I have a sqlite query that I\'m trying to write. I have two tables:

TableA (sales): id sales date

TableB (goals): id goal date

I\'m selecting from Ta

3条回答
  •  忘掉有多难
    2020-12-19 16:03

    SELECT a.id, a.sales, a.date, (SELECT TOP 1 Goal 
                                   FROM TableB b WHERE b.date < a.date
                                   ORDER BY b.date DESC) As Goal
    FROM TableA a
    

    Going off the nearest date in the past.

提交回复
热议问题