Php 存储过程中的循环
我正在尝试编写一个过程,该过程将触发相同的select查询,直到结果数超过0。如果间隔2小时返回0条记录,则应使用间隔4小时标准,如果仍然没有提取记录,则 应在where子句中使用lastupdate>current_date 以下是过程中使用的两个基本查询Php 存储过程中的循环,php,sql,mysql,Php,Sql,Mysql,我正在尝试编写一个过程,该过程将触发相同的select查询,直到结果数超过0。如果间隔2小时返回0条记录,则应使用间隔4小时标准,如果仍然没有提取记录,则 应在where子句中使用lastupdate>current_date 以下是过程中使用的两个基本查询 select sql_calc_found_rows id from sometable where lastupdate > date_sub(now(), interval 2 hour) limit 10; select fo
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$$
从MySQL文档:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
...
SET v1 = v1 - 1;
END WHILE;
END
PHP页面将由用户触发,因此您必须将结果保存到数据库中,直到有人打开显示结果的页面
您可以使用PHP cronjob,而不是在数据库中使用无限循环。根据您的情况,最好在数据库上设置触发器。这使您可以仅在相关数据更改时执行任何必要的工作/重新计算,而不是在数据库中没有任何更改的情况下通过不断轮询使服务器变得不必要的繁忙。这是我第一次尝试在StackOverflow上发布回复。希望对你有帮助 下面我使用本地MySQL数据库编写了一个测试过程。它被设置为运行10整秒,如果没有记录,则返回空结果集。如果在运行的10秒内插入一行,它将退出循环并返回新结果 Sleep函数用于防止程序在运行时消耗过多CPU,方法是每秒只运行一次Select Count*。您可以将其设置为所需的任何间隔 虽然这个过程运行良好,但我必须同意natevw的使用方法
DELIMITER $$
CREATE PROCEDURE sp_TestQueryResultsTimeout()
BEGIN
DECLARE v_interval, ct, v_time INT;
/* This will keep track of how much time has passed*/
SET v_interval = 0;
/* This is used for comparing the rowcount*/
SET ct = 0;
/* This is used to keep the procedure from returning the Sleep functions results'
This could also be used to keep a more accurate count of the amount of Sleep time has passed by adding the results of the sleep function to it on every iteration*/
SET v_time = 0;
/* This while statement should run for slightly longer than ten seconds
The amount of extra time will begin to add up depending on how long the select count (*) takes
and how many iterations you want to make'*/
WHILE v_interval < 10 DO
/*Get the count from your table*/
SET ct = (SELECT count(*) FROM tbUsers);
/*If the count is greater than 0, exit the while by satisfying the condition*/
if (ct > 0) then
SET v_interval = 10;
else
/*If the count is less than 0, sleep for 1 second*/
SET v_time = (SELECT SLEEP(1));
end if;
/*Increment*/
SET v_interval = v_interval + 1;
END WHILE;
SELECT * FROM tbUsers;
END$$
DELIMITER ;
我突然想到,虽然您要求在文本的标题和正文中都有一个循环,但您真正想做的是在过去的X小时内修改行列表,最小的X返回一些非空的行集 以下是实现这一目标的一种方法:
delimiter $$
create procedure recently_modified_rows()
begin
declare tablelastupdate int; -- how many hours ago table was last updated.
declare showperiod datetime; -- what time interval to show
declare showtext text; -- text describing time interval
select hour(timediff(now(), max(lastupdate))) into tablelastupdate
from sometable;
if(tablelastupdate < 2)
set showperiod = time_sub(now(), interval 2 hours);
set showtext = "modified in the last 2 hours";
elseif (tablelastupdate < 4)
set showperiod = time_sub(now(), interval 4 hours);
set showtext = "modified in the last 4 hours";
else
set showperiod = current_date();
set showtext = "modified today";
end if
select sql_calc_found_rows id,
showtext description
from sometable
where lastupdate > showperiod
limit 10;
end$$
你为什么要做这样的事?你能描述一下这个场景吗?你想研究一下使用WHILE循环:为什么?航空公司飞行计划;尝试在2小时时段内查找航班,如果2小时时段内没有航班,则返回4小时,如果4小时时段内没有航班,则返回全天。作为一个“例子”,当然。。。
$query = mysql_query("call recently_modified_rows()") or die( mysql_error() );
$numrows = mysql_numrows($query);
if ($numrows != 0)
{
/* print out what time interval we used */
$description = mysql_result($query, 0, 'description');
echo "Found $numrows rows $description";
/* print out the rows */
while ($row = mysql_fetch_array($query))
{
echo "Id: {$row['id']}";
}
}
else
{
/* happens only if there were no records modified in any of the three versions */
echo "no records were modified in the last 2 hours, 4 hours, or today";
}