林具有這些表分貝的存儲過程:試圖寫有開始時間和結束時間
drop table if exists events1;
create table if not exists events1(
time_stamp decimal(16,6),
message char(90) not null default 'defult message'
);
drop table if exists events2;
create table if not exists events2(
time_stamp decimal(16,6),
message char(90) not null default 'defult message'
);
drop table if exists events3;
create table if not exists events3(
time_stamp decimal(16,6),
message char(90) not null default 'defult message'
);
insert into events1 (time_stamp,message) values (1485193300,"a1");
insert into events1 (time_stamp,message) values (1485193600,"b1");
insert into events1 (time_stamp,message) values (1485193900,"c1");
insert into events2 (time_stamp,message) values (1485193300,"a1");
insert into events2 (time_stamp,message) values (1485193600,"b2");
insert into events2 (time_stamp,message) values (1485193900,"c3");
insert into events3 (time_stamp,message) values (1485193300,"a1");
insert into events3 (time_stamp,message) values (1485193600,"b2");
insert into events3 (time_stamp,message) values (1485193900,"c3");
我試圖建立一個存儲過程,有3個參數:
start_time (int) (in epoch)
end_time (int) (in epoch)
interval (int) (in epoch)
我不希望這個程序會迭代start_time,步驟爲區間直到end_time,並在每次迭代時,根據步驟和時間間隔獲得三個表的並集。
類似:
DELIMITER //
CREATE PROCEDURE get_events
(IN start_time INT,
IN end_time INT,
IN interval INT,
)
BEGIN
while [ start_time <= end_time]
do
next_time = start_time + interval
select *
from events1
where start_time <= time_stamp and time_stamp <= next_time
union
select *
from events2
where start_time <= time_stamp and time_stamp <= next_time
union
select *
from events3
where start_time <= time_stamp and time_stamp <= next_time
start_time = start_time + interval
end while
END //
DELIMITER ;
我知道我有語法錯誤,我不知道我怎樣才能返回結果,並在WHERE情況下給定的時間間隔遍歷3代表的聯合。
請指教。
你所說的「返回結果」是什麼意思? 'UNION'將爲WHILE每次迭代返回一個結果集。 –