【问题标题】:Inserting multiple rows into a single row mysql将多行插入单行mysql
【发布时间】:2014-09-25 23:39:57
【问题描述】:

以下脚本旨在具有以下功能:

  1. 在eurusd_m1 中找到volume=250 的一行,
  2. 找到第 1 行之前的行,
  3. 找到第 1 行之后的行,
  4. 从这些行中的每一行复制所需的值到 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


【解决方案1】:

用这段冗长的代码解决了问题。我想我可以稍微简化一下,所以我很乐意接受建议。

drop procedure if exists doji_result; 
DELIMITER $$
Create procedure doji_result ()
begin 

DECLARE final DATETIME;
DECLARE centre DATETIME;
DECLARE openB1 DOUBLE;
DECLARE highB1 DOUBLE;
DECLARE lowB1 DOUBLE;
DECLARE closeB1 DOUBLE;
DECLARE volumeB1 DOUBLE;
DECLARE openA1 DOUBLE;
DECLARE highA1 DOUBLE;
DECLARE lowA1 DOUBLE;
DECLARE closeA1 DOUBLE;
DECLARE volumeA1 DOUBLE;

DECLARE x int;

SET x = 0;

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);

SET @openB1 = (select open from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @highB1 = (select high from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @lowB1 = (select low from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @closeB1 = (select close from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);
SET @volumeB1 = (select volume from eurusd_m1 where MQLtime < @centre order by mqltime desc limit 1);

SET @openA1 = (select open from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @highA1 = (select high from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @lowA1 = (select low from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @closeA1 = (select close from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 1);
SET @volumeA1 = (select volume from eurusd_m1 where MQLtime > @centre order by mqltime asc limit 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; 

update obsh4
SET open2 = @openB1,
    high2 = @highB1,
    low2 = @lowB1,
    close2 = @closeB1,
    volume2 = @volumeB1,
    open3 = @openA1,
    high3 = @highA1,
    low3 = @lowA1,
    close3 = @closeA1,
    volume3 = @volumeA1
order by mqlreftime desc limit 1;

SET x=x+1;

UNTIL @centre=@final
END REPEAT;

END $$
DELIMITER ;

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2015-02-28
    • 2012-08-01
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多