mysql - mysql 将多行插入单行

标签 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; 
    

我可以看到三个选项:

1)更新语句需要修正

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

3)从头开始分箱并生成一些东西。

我们非常欢迎对其中任何一个提出任何建议。 谢谢

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

最佳答案

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

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 ;

关于mysql - mysql 将多行插入单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25102743/

相关文章:

php - 可以在 WHERE 子句中评估列别名吗?

mysql - 如何在mysql表中找到一定数量的重复项?

PHP 或 MYSQL : how to group results by ROW?

sql - 新来的sql,如何将列值插入到新列中?

c++ - 将节点添加到链表的末尾

mysql - 基于 PHP 5.2 的应用程序无法连接到 Google Cloud SQL

C++捕获命令行逐行输入和输出

c++ - 如何在不使用堆栈或数组的情况下从 int 中提取每个数字? (C++)

python - 使用迭代变量检查字典键的值?

php - 如何在PHP的sqlite中插入新行?