2017-01-23 5 views
0

Im имея дб с этими таблицами:Попытка написать хранимую процедуру с временем начала и окончания

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, с шагом интервал до конца_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 ; 

я знаю, что есть синтаксические ошибки, я не знаю, как я могу вернуть результаты и перебрать объединения 3 таблицы с заданными интервалами в случае, когда ,

сообщите пожалуйста.

+0

Что вы имеете в виду под «возвращать результаты»? 'UNION' вернет набор результатов для каждой итерации' WHILE'. –

ответ

0

Каковы синтаксические ошибки?

[ недопустимый синтаксис; возможно ( в WHILE?

a = a + b недопустимый синтаксис; см. команду SET.

Имеет 3 стола с одинаковой схемой обычно плохой дизайн.

Использование CHAR вместо VARCHAR us обычно плохая идея.

+0

Я написал схему только для иллюстрации. Я застрял в хранимой процедуре. я написал время в своем более как псевдокод, я не знаю, как сохранить результаты каждого итерационного запроса и как вернуть все результаты. –

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

 Смежные вопросы

  • Нет связанных вопросов^_^