【发布时间】:2014-09-25 23:39:57
【问题描述】:
以下脚本旨在具有以下功能:
- 在eurusd_m1 中找到volume=250 的一行,
- 找到第 1 行之前的行,
- 找到第 1 行之后的行,
-
从这些行中的每一行复制所需的值到 obsh4 中的一行
Create procedure doji_result () begin DECLARE initial DATETIME; DECLARE final DATETIME; DECLARE centre DATETIME; DECLARE x int; SET x = 0; SET @initial = (select MQLTime from eurusd_m1 where volume=250 order by mqltime asc limit 1); SET @final = (select MQLTime from eurusd_m1 where volume=250 order by mqltime desc limit 1); REPEAT SET @centre = (select MQLTime from eurusd_m1 where volume=250 order by mqltime asc limit x,1); INSERT INTO obsh4 (MQLrefTime,RrefTime,Open,High,Low,Close,Volume) select MQLTime,RTime,Open,high,Low,Close,Volume from eurusd_m1 where MQLTime = @centre order by MQLTime asc limit 1; INSERT INTO obsh4 (Open2,High2,Low2,Close2,Volume2) select Open,high,Low,Close,Volume from eurusd_m1 where MQLTime < @centre order by MQLTime desc limit 1; INSERT INTO obsh4 (Open3,High3,Low3,Close3,Volume3) select Open,high,Low,Close,Volume from eurusd_m1 where MQLTime > @centre order by MQLTime asc limit 1; SET x=x+1; UNTIL @centre=@final END REPEAT; END $$ DELIMITER ;当我运行此命令时,eurusd_m1 中的每一行都被复制到 obsh4 中的新行(而不是在每次循环迭代时将所有三行折叠成一行)
我尝试使用 UPDATE 使用以下脚本添加第二行和第三行数据,但是我在 FROM 上遇到语法问题,我也不知道如何解决这个问题:/
update obsh4 set Open2 = open, High2 = high, Low2 = low, Close2 = close, Volume2 = volume, from select Open,high,Low,Close,Volume from eurusd_m1 where MQLTime < @centre order by MQLTime desc limit 1;
我可以看到三个选项:
1) 更新语句需要修正
2) 需要在第二/第三行的插入中添加行规范
3) 从头开始分箱和生成一些东西。
任何关于其中任何一个的建议都将受到欢迎。 谢谢
编辑:循环本身运行正常,并且已经独立于当前问题进行了测试。
【问题讨论】:
-
我也尝试查看内部连接块来解决此问题,但我收到错误 1221(错误使用更新和排序依据)。
-
我相信这个 (xaprb.com/blog/2006/08/10/…) 可能有用,并且在这里也可以参考 SO (stackoverflow.com/questions/10544502/…)。我正在尝试按如下方式运行:
-
{将 obsh4 更新为目标内连接(从 obsh4 中选择 ta.MQLrefTime 作为 ta 内连接 temph4 作为 tb on tb.MQLTime = ta.MQLrefTime where tb.MQLTime
标签: mysql loops insert sql-update multiple-columns