将多行插入单行mysql

时间:2014-08-03 07:57:15

标签: mysql loops insert sql-update multiple-columns

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

  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; 
    
  5. 我可以看到三个选项:

    1)需要纠正更新声明

    2)需要在插入第二行/第三行时添加行规范

    3)从头开始装箱并生成东西。

    对这两种方面的任何建议都是最受欢迎的。 谢谢

    编辑:循环本身运行正常,并且已经过独立于当前问题的测试。

1 个答案:

答案 0 :(得分:0)

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

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 ;