简体   繁体   English

将多行插入单行mysql

[英]Inserting multiple rows into a single row mysql

The following script is intended to have the following functions: 以下脚本旨在具有以下功能:

  1. find a row in eurusd_m1 where volume=250, 在eurusd_m1中找到一行,其中volume = 250,
  2. find the row before row 1, 在第1行之前找到行
  3. find the row after row 1, 在第一行之后找到第一行
  4. copy the required values from each of these rows into a single row in obsh4 将每行中的所需值复制到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 ; 

    When I run this each row from eurusd_m1 is copied to a new row in obsh4 (rather than all three rows being collapsed into a single row with each loop iteration) 当我运行此代码时,eurusd_m1中的每一行都将被复制到obsh4中的新行中(而不是在每次循环迭代时将所有三行都折叠成一行)

    I tried to use UPDATE for adding the second and third row of data using the following script however I get a syntax issue on FROM and I'm not sure how to remedy that either :/ 我尝试使用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; 

I can see three options: 我可以看到三个选项:

1) the update statement needs to be remedied 1)更新语句需要纠正

2) there needs to be row specification added to the insertion of the second/third rows 2)第二行/第三行的插入需要添加行规范

3) binning and generating something from scratch. 3)分箱并从头开始生成某些东西。

Any suggestions on either of these would be most welcome. 关于这两个方面的任何建议都将受到欢迎。 Thank you 谢谢

Edit: the loop itself operates correctly and has been tested independently of the current problem. 编辑:循环本身正常运行,并且已独立于当前问题进行了测试。

Problem answered with this lengthy piece of code. 这段冗长的代码解决了问题。 I think I could be simplified somewhat so I am well open to suggestions. 我想我可以简化一些,所以我很愿意提出建议。

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM