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.
Bu prosedürde kullanılan 2 temel sorgular.
select sql_calc_found_rows id from sometable where lastupdate > date_sub(now(), interval 2 hour) limit 10;
select found_rows();
+--------------+
| found_rows() |
+--------------+
| 41 |
+--------------+
Aşağıdaki yordam doğru mu? Bir SP yazmak için doğru yolu nedir? Ve nasıl bir PHP kodu sonuçlarını kullanabilirim?
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$$