gpt4 book ai didi

mysql - MySQL 按日期连接两个表

转载 作者:行者123 更新时间:2023-11-29 21:09:17 24 4
gpt4 key购买 nike

我有这个:

SELECT * FROM history JOIN value WHERE history.the_date >= value.the_date

是否可以以某种方式提出这样的问题:history.the_date 大于或等于 value.the_date 的最大可能值?

HISTORY
the_date amount
2014-02-27 200
2015-02-26 2000

VALUE
the_date interest
2010-02-10 2
2015-01-01 3

我需要将正确的利息与金额配对!

最佳答案

因此,value.the_date 是利息有效的日期。利息 2 的有效期为 2010 年 2 月 10 日至 2014 年 12 月 31 日,因为自 2015 年 1 月 1 日起,新的利息 3 适用。

要获取某个日期的当前兴趣,您可以使用子查询,在其中选择具有截至该日期的有效起始日期的所有兴趣记录,并仅保留最新的:

select
the_date,
amount,
(
select v.interest
from value v
where v.the_date <= h.the_date
order by v.the_date desc
limit 1
) as interest
from history h;

关于mysql - MySQL 按日期连接两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com