Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 用于更新百分比更改的SQL语句_Mysql_Sql_Sql Update - Fatal编程技术网

Mysql 用于更新百分比更改的SQL语句

Mysql 用于更新百分比更改的SQL语句,mysql,sql,sql-update,Mysql,Sql,Sql Update,我在S.O.搜索了这个答案,已经接近答案,但仍然不够接近。我很想知道MySQL是否有这个功能 我已经用Perl和MySQL 4进行了开发,现在正在使用MySQL 4。 我的桌子看起来像这样 符号varchar25 今天 兴趣国际11 我的问题是……这些符号中大约有200000个每天都会用新的兴趣域编号进行更新 例如 symbol | todayDate | interest ------------------------------- A202015 | 2010-10-26 | 150

我在S.O.搜索了这个答案,已经接近答案,但仍然不够接近。我很想知道MySQL是否有这个功能

我已经用Perl和MySQL 4进行了开发,现在正在使用MySQL 4。 我的桌子看起来像这样

符号varchar25 今天 兴趣国际11 我的问题是……这些符号中大约有200000个每天都会用新的兴趣域编号进行更新

例如

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
理想情况下,我能做的是在最后更新另一个字段,与以前的记录相比有一个百分比的变化。那么,上面的内容将是这样的

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天才有什么智慧让我通过。

从样本数据中,我假设记录没有更新,而是插入了新的记录

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是表的名称-替换为实际值

这有点奇怪,因为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;
哦,我应该补充一下,这是针对mysql 5.x数据库服务器的。我不确定它是否能在4.x环境下工作,因为我没有4.x服务器可供测试,对不起


-don

在与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 ;

谢谢你,埃兰!这是一个良好的开端,我非常感谢你的帮助。JWDon,你是个圣人,我感谢你的帮助。JWI已经接受了这个答案,它完全按照指示工作。完美!完美的完美的完美的完美的完美的再次谢谢你,唐!珍妮