我一直在研究这个问题,这似乎太复杂了。我想要做的是使用以下操作更新列(x
):(p * (100/c) ) / 100
。
p
对应于日期x
的值,c
对应于日期x
减去一天的值。
我尝试使用循环创建存储过程但是select语句在循环语句中对我不起作用。
这是我的更新程序:
BEGIN
DECLARE firstqDate,date2 date;
DECLARE p, c float;
DECLARE cpt, val int;
SET @val = 0;
SET @cpt = (select count(*)-1 from quotes);
WHILE (val < 3) DO
SET @firstqDate = (select qDate from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1);
SET date2 = (select qDate from quotes where qDate like DATE_ADD(@firstqDate, INTERVAL 1 DAY );
SET p = (select qOp from quotes where qDate like date2);
SET c = (select qCl from quotes where qDate like DATE_SUB(date2, INTERVAL val DAY));
update quotes
set qCh = (p * (100/c) ) / 100;
set val = val + 1;
end while;
END
编辑:我对存储过程进行了一些更新,但仍然没有更新行!
BEGIN
DECLARE firstqDate,date2 date;
DECLARE p, c float;
DECLARE cpt, val int;
SET @val = 0;
SET @cpt = (select count(*)-1 from quotes);
SET firstqDate = (select qDate from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1);
WHILE (val < 3) DO
SET date2 = (select qDate from quotes where qDate like DATE_ADD(@firstqDate, INTERVAL val DAY ));
SET p = (select qOp from quotes where qDate like date2);
SET c = (select qCl from quotes where qDate like DATE_SUB(date2, INTERVAL val+1 DAY));
set val = val + 1;
update quotes
set qCh = (p * (100/c) ) / 100
where qOp = p AND qCl = c;
end while;
END
我再次对存储过程进行了一些更新,但没有更改。我使用了一些功能。
BEGIN
DECLARE p, c float;
DECLARE cpt, val int;
SET @val = 0;
SET @cpt = (select count(*)-1 from quotes);
WHILE (@val < 3) DO
SET p = getp(@val, getd());
SET c = getc(@val+1, getd());
set @val = @val + 1;
update quotes
set qCh = (@p * (100/@c) ) / 100
where qOp = @p AND qCl = @c;
end while;
END
功能: 得到p:
BEGIN
declare d date;
select qDate into d from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1;
return d;
END
获取c:
BEGIN
DECLARE c float;
DECLARE qDa date;
select qDate into qDa from quotes where qDate like DATE_SUB(qD, INTERVAL v DAY );
SELECT qCl INTO c FROM quotes WHERE qDate = qDa;
RETURN c;
END
GETD:
BEGIN
declare d date;
select qDate into d from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1;
return d;
END
此存储过程必须从qDate的p和qDate的c减去一天计算所有qCh。 谢谢!
编辑 - 解决了
OUF!我终于设法编写了这个存储过程:
BEGIN
DECLARE p, c float;
DECLARE cpt, val int;
SET @val = 0;
SET @cpt = (select count(*)-1 from quotes);
WHILE (@val <= 2) DO
SET p := getp(@val, getd());
SET c := getc(@val+1, getd());
set @val := @val + 1;
update quotes q
set q.qCh = (getp(@val, getd()) * (100/getc(@val-1, getd())) ) / 100
where q.qOp = getp(@val, getd());
end while;
END
new getC
BEGIN
DECLARE c float;
DECLARE qDa date;
select qDate into qDa from quotes where qDate like DATE_ADD(qD, INTERVAL v DAY );
SELECT qCl INTO c FROM quotes WHERE qDate = qDa;
RETURN c;
END
我在第一次迭代中将函数getC的代码更改为add(-1)。现在它正在运作! 谢谢大家的帮助!
答案 0 :(得分:0)
你的更新周期看起来不错,即使我不明白为什么你做了3个周期。你不应该使用:
WHILE (@val < 3) DO
而不是
WHILE (val < 3) DO
?希望它有所帮助
编辑: 您需要调试周期以了解问题所在。
试试这个:
BEGIN
DECLARE @p, @c float;
DECLARE @cpt, @val int;
SET @val = 0;
SET @cpt = (select count(*)-1 from quotes);
SELECT 'Enter Cycle';
WHILE (@val < 3) DO
SELECT 'In Cyle';
SET @p = getp(@val, getd());
SELECT @p;
SET c = getc(@val+1, getd());
set @val = @val + 1;
update quotes
set qCh = (@p * (100/@c) ) / 100
where qOp = @p AND qCl = @c;
end while;
END
你是否打印'输入循环'和'在Cyle'? p Variable的值是否打印?这是对的吗?