gpt4 book ai didi

php - 运行插入查询最多 5 分钟 MySql

转载 作者:行者123 更新时间:2023-11-29 12:25:25 25 4
gpt4 key购买 nike

基本上我有一个 MySql 表位置

------    -------------
b_id updated_time
------ -------------
C1 17-5-2014 16:55:35
D1 17-5-2014 16:55:32

还有另一个表location_history

 ------    -------------
b_id updated_time
------ -------------
C1 17-5-2014 16:55:35
C1 17-5-2014 16:55:34
C1 17-5-2014 16:55:33
D1 17-5-2014 16:55:32
D1 17-5-2014 16:55:31
D1 17-5-2014 16:55:30

这里位置表中的数据将被插入一次并不断更新。然后location_history表中的数据不断插入新记录而不是更新。

我们可以在位置表中找到最新更新的时间。 C1 为 16:55:35,D1 为 16:55:32。这些插入是随机的

我想要的是 - 根据位置表中的最新更新时间,我想在 location_history 表中运行 INSERT 查询 5 分钟。

假设,我有新的 b_id -> E1,其位置表中的 update_time 为 16:55:37。这是最新的。因此,在 5 分钟内,INSERT 查询应该继续在我的 location_history 表上执行 b_id=E1

最佳答案

在这种情况下,每次插入位置表后都会调用此代码(使用触发器或仅在 PHP 脚本中调用)

我想您的 PHP >= 5.3.0

...
$check_time = time();
$up_to_time = strtotime("+ 5 minutes");

while( $check_time < $up_to_time ){

$check_time = time();
$updated_time = date("Y-m-d H:i", $check_time);

// $b_id is an inserted id got from INSERT into location table
$sql = "INSERT INTO location_history ( b_id, updated_time )
VALUES( $b_id, '$updated_time');

mysql_query($sql) or die(mysql_error();
sleep(50);

}

关于php - 运行插入查询最多 5 分钟 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com