php - 如何在 MySQL 上运行及时自动查询

标签 php mysql

我有一个名为 pending 的字段,并被声明为 bool 值,默认值为 0 如下:

`pending` tinyint(1) NOT NULL DEFAULT '0'

我正在对数据库运行更新查询,以将 qpending 的状态更改为 1,如下所示

$sql = "UPDATE  `appointments` SET  `pending` =  '1' WHERE  `appointments`.`id` = 124;

现在我的问题是,有没有办法通过采用条件子句(例如

)在 30 分钟后自动将 pending 重新声明为 0
// After 30 Minutes of update!
if (!confirmed){
    $sql = "UPDATE  `appointments` SET  `pending` =  '0' WHERE  `appointments`.`id` = 124;
}

表架构

CREATE TABLE IF NOT EXISTS `appointments` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `date` varchar(100) CHARACTER SET utf8 NOT NULL,
  `available` tinyint(1) NOT NULL DEFAULT '1',
  `pending` tinyint(1) NOT NULL DEFAULT '0',
  `confirmed` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

最佳答案

你做错了。

使您的待处理字段不是 bool 值,而是日期时间,并包含到期时间的值。然后在您的选择查询中只需将该值与当前时间进行比较。所以而不是

SELECT * FROM appointments WHERE pending = 1

只要做到

SELECT * FROM appointments WHERE pending < NOW()

这个解决方案要么更简单,要么更灵活

关于php - 如何在 MySQL 上运行及时自动查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42199668/

相关文章:

php - Yii 2.0 : 2 database connection

mysql - 选择 Mysql 数据类型

python - django+mysql ='DatabaseWrapper' object has no attribute 'Database'错误

php - mysql isnumeric sql注入(inject)

php - fputcsv 写入空白文件

php - Laravel 按日期排序集合

php - 使用 jquery 删除列表项 <li>?

php - Sublime Text 3 错误 : Warning. 需要 PHP 5.6 或更新版本。请升级您的本地 PHP 安装

php - elastic beanstalk 使用 cron.yaml 设置 php crontab

php - 与 MariaDB 服务器建立连接,但无法将数据存入数据库?