I am trying to write a procedure that will fire the same select query till the number of results are more than 0. If the "interval 2 hour " returns 0 records then the "interval 4 hour" criterion should be used and if there are still no records fetched, then 
lastupdate > current\_date() should be used in the where clause.

These are the 2 basic queries used in the procedure.

```
select sql_calc_found_rows id from sometable where lastupdate > date_sub(now(), interval 2 hour) limit 10;

select found_rows();
+--------------+
| found_rows() |
+--------------+
|           41 | 
+--------------+

```

Is the following procedure correct? Is it the right way to write a SP? And how do I use the results in the PHP code?

```
delimiter $$
create procedure mytest3()
begin 

  declare myvar int;

 select sql_calc_found_rows id 
   from sometable 
  where lastupdate > date_sub(now(), interval 2 hour) 
  limit 10;

 select found_rows() into myvar;

if (myvar > 0) 
  then select 'Found in 2 hours';
else
  select sql_calc_found_rows id 
    from sometable 
   where lastupdate > date_sub(now(), interval 4 hour) 
   limit 10;

select found_rows() into myvar;

if (myvar > 0) 
  then select 'Found in 4 hours';
else 
  select sql_calc_found_rows id 
    from sometable 
   where lastupdate > current_date() 
   limit 10;
end if;
end if; 
end$$ 

```