Join two tables where table A has a date value and needs to find the next date in B below the date in A

后端 未结 3 1308
情深已故
情深已故 2021-01-26 12:50

I got this table \"A\":

| id | date       |
===================
| 1  | 2010-01-13 |
| 2  | 2011-04-19 |
| 3  | 2011-05-07 |
| .. | ...        |

3条回答
  •  不要未来只要你来
    2021-01-26 13:40

    You could use a subquery with limit 1 to look up the latest value in table B:

    select  id
    ,       date
    ,       (
            select  value
            from    B
            where   B.date < A.date
            order by
                    B.date desc
            limit   1
            ) as value
    from    A
    

提交回复
热议问题