我正在嘗試編寫一個過程,它將觸發相同的select查詢,直到結果數大於0.如果「interval 2 hour」返回0個記錄,則「interval 4 hour 「標準,並且如果仍沒有記錄被提取,則應在where子句中使用lastupdate> current_date()。直到在存儲過程中循環
這些是過程中使用的2個基本查詢。
select sql_calc_found_rows id from sometable where lastupdate > date_sub(now(), interval 2 hour) limit 10;
select found_rows();
+--------------+
| found_rows() |
+--------------+
| 41 |
+--------------+
以下步驟是否正確?這是寫SP的正確方法嗎?我如何在PHP代碼中使用結果?
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$$
爲什麼你想要做這樣的事情?你能描述一下這個場景嗎? – shahkalpesh 2009-10-29 06:44:38
你想看看使用WHILE循環:http://dev.mysql.com/doc/refman/5.0/en/while-statement.html – 2009-10-29 06:44:55
爲什麼?航空公司飛行計劃;試圖在2小時的時間段內找到航班,如果在2小時的時間段內沒有任何時間段,則會在4小時後回落,如果在4小時的時間段內沒有任何時間,則會在整天后退。作爲一個「例如」,當然...... – 2009-11-07 15:27:26