gpt4 book ai didi

mysql - 更新百分比变化的 SQL 语句

转载 作者:可可西里 更新时间:2023-11-01 08:22:19 25 4
gpt4 key购买 nike

我搜索过 S.O.对于这个答案并且已经接近答案但仍然不够接近。我很想知道 MySQL 是否具有此功能。

我用 Perl 和 MySQL 4 开发过,现在正在使用 MySQL 4。我的 table 看起来像这样......

  • 符号 varchar(25)
  • todayDate 日期
  • 兴趣 int(11)

我的问题是......这些符号(大约 200,000 个)每天都会更新一个新的兴趣字段数字。

一个例子是这个....

symbol  | todayDate  | interest
-------------------------------
A202015 | 2010-10-26 | 150
A202015 | 2010-10-25 | 100

理想情况下,我能够做的是在末尾更新另一个字段,其中包含与先前记录相比的百分比变化。上面的内容看起来像这样....

symbol  | todayDate  | interest | change
-----------------------------------------
A202015 | 2010-10-26 | 150 | 50
A202015 | 2010-10-25 | 100

我不认为这个功能在 MySQL 中是可能的。我得出的结论是,我只需要获取以前的记录信息,进行数学运算,然后使用百分比信息更新最新记录。我只是想我会仔细检查一下,看看是否有任何 MySQL 天才有智慧让我通过。

最佳答案

在与 Wilkie 女士通过电子邮件交谈后,发现她想要这样的百分比变化:

update t_test_1 as t1 
set chng = (t1.interest - (
select interest from (
select *
from t_test_1 as t11
) as x
where x.symbol = t1.symbol and x.todayDate < t1.todayDate
order by x.todayDate desc
limit 1
)) /
(
select interest from (
select *
from t_test_1 as t11
) as x2
where x2.symbol = t1.symbol and x2.todayDate < t1.todayDate
order by x2.todayDate desc
limit 1
) * 100 ;

关于mysql - 更新百分比变化的 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4029688/

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