Join tables on nearest date in the past, in MySQL

前端 未结 3 749
别那么骄傲
别那么骄傲 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:23

    Just get the cartesian product and then filter out uninteresting rows. Basically:

    select a.*, b.*
    from a, b
    where
    not exists (
      select null
      from b b2
      where abs(b2.date - a.date) < abs(b.date - a.date)
    )
    

    I don't know if SQLite supports that, tough.

提交回复
热议问题