我已经搜索了S.O.的答案,并接近了答案,但仍然不够接近。我想知道MySQL是否有这种能力。
我已经用Perl和MySQL 4开发了,我现在使用的是MySQL 4。我的桌子看起来像..。
varchar(25)
我的问题是this.....these符号(其中大约有2000个)每天更新一个新的兴趣字段数字。
一个例子就是这个..。
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天才有任何智慧通过我的方式。
发布于 2010-11-01 13:52:27
在与威尔基女士通过电子邮件交谈后,事实证明她想要像这样的百分之一的改变:
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 ;发布于 2010-10-27 03:35:34
这有点奇怪,因为MySQL引用子查询的方式,但这可以满足您的需要--我认为:
/*
create table t_test_1 (
symbol varchar(20) not null,
todayDate datetime not null,
interest int not null,
chng int null
)
*/
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-09', 90, null);
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-10', 80, null);
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-11', 120, null);
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 * from t_test_1;这导致:
A202015 2010-10-09 90 NULL
A202015 2010-10-10 80 -10
A202015 2010-10-11 120 40哦,我应该说,这是针对MySQL5.x数据库服务器的。我不确定它是否对4.x有效,因为我没有一个4.x服务器来测试,对不起。
-don
发布于 2010-10-27 03:14:24
从示例数据中,我假设记录不是“更新”的,而是插入了新的记录。
INSERT INTO `rates` (`symbol`,`todayDate`,`interest`,`change`)
SELECT symbol,CURDATE(),$interest,$interest - `interest`
FROM `rates`
WHERE `symbol`='$symbol' AND `todayDate` = CURDATE() - INTERVAL 1 DAY($interest和$symbol是包含要插入的值的变量,rates是表的名称-用实际值替换)
https://stackoverflow.com/questions/4029688
复制相似问题