1
drop procedure if exists findClosestTimeStamp;
delimiter $$
create procedure findClosestTimeStamp()
begin
   declare tt1 time;
   declare dt1 date;
    declare  view1 varchar(30);
    declare done int default false;
    declare curComm cursor for
        select * from t1; -- This is the query used by the cursor.
    declare continue handler for not found -- This handler will be executed if no row is found in the cursor (for example, if all rows have been read).
        set done = true;

   
    open curComm;
  
    loop_comm: loop
      
        fetch curComm into dt1,tt1, view1;
      
        if done then
            leave loop_comm;
        end if;
       
        SELECT * FROM logs
        where tt <= tt1 -interval 1 minute;
    end loop;
close curComm;
end $$
delimiter ;
call findClosestTimeStamp();

Table t1 has

dt         tt       view
2020-06-26 16:47:03 TouchDisabled
2020-06-26 07:58:46 Toucbled
2020-06-26 07:58:48 Touchsabled
2020-06-26 07:58:49 TouchUpled

Table logs has

AppName Button      dt          tt
App1    Button1     2020-06-26  07:45:37    
App2    Button2     2020-06-26  07:45:42    
App3    Button3     2020-06-26  07:45:44    

I am trying to fetch data which occur one minute before the time attribute pointed by cursor in table t1 from the logs table. I dont understand why this is code is printing same value 3 times .and also not doing what I am trying to do!!

edit 1:

when cursor is pointing on the first row of table t1 I will use this tables time into the variable tt1 ,Now using this tt1 time I want to fetch the data in logs table. The data to be fetched should be betwwen tt1-1 minute to tt1

example:

if I take row 1 time 16:47:03 so it should output the logs which are having tt values between 16:46:03 to 16:47:03. I hope this clears question more!!

6
  • Please 1) Replace table-formatted example data with its CREATE TABE + INSERT INTO scripts 2) Show desired output for this sample data with explainatons why so 3) Select ONLY ONE DBMS (looking on the code - this will be MySQL), specify its version, remove excess DBMS tags. fetch data which occur one minute before The distance between max time in logs table and min time in t1 table is over 1 min - the output will be empty.
    – Akina
    Commented Jun 28, 2020 at 9:18
  • @Akina I have made edits ,the logs and t1 table is not populated completely I want to do that when table is completely populated.I am using MySQL version 8.0.
    – Nurav
    Commented Jun 28, 2020 at 9:29
  • @Akina It will be great help if you can solve my problem!!
    – Nurav
    Commented Jun 28, 2020 at 9:33
  • I don't understand your task. And you hide it carefully, showing the way which you decide to use for the task solving only. And it seems that this way is not correct. For example, if you need to get all rows from t1 table, and each row must be accompanied by all rows from logs table which belongs in a minute range, then SP not needed, one simple query may solve it.
    – Akina
    Commented Jun 28, 2020 at 9:40
  • my task is like when i hit a disabled button i want to find the buttons ??? What buttons - in MySQL??? It seems that you have a list of tasks: in some client - detect what button is disabled, get its index (?) and transter it to MySQL as a parameter of a query or SP; in MySQL - receive some value (index), find and return the rows in logs table which matches the condition for received index value: in client - receive found rows (theis index values?) and do somethind with according rows on the screen. Extract the task dealed with MySQL only from your global task,, and solve it there.
    – Akina
    Commented Jun 28, 2020 at 9:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.