[英]Inserting multiple rows into a single row mysql
以下腳本旨在具有以下功能:
將每行中的所需值復制到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 ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.