我有这些表的数据库:
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 ,步骤 interval 直到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个表的并集。
请告知。
答案 0 :(得分:0)
语法错误是什么?
[
是无效的语法;可能是(
中的WHILE
?
a = a + b
是无效的语法;请参阅SET
命令。
拥有3个具有相同架构的表是通常一个糟糕的设计。
使用CHAR
代替VARCHAR
我们通常一个坏主意。
答案 1 :(得分:0)
INSERT INTO "table name"
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
这会将查询存储在表中,然后您可以遍历该表。不知道那是不是你要的。
以下是文档: https://dev.mysql.com/doc/refman/5.7/en/insert-select.html