我想制作一个每小时执行的cronjob,从两个表中选择,如果在两个表中都找不到记录且超过2小时 - >>删除它。
到目前为止,我已经构建了这个简单的删除语句,但我意识到这将删除超过2小时的所有记录,因为我不检查第二个表。所以我需要一点帮助,我可以选择和if()然后删除..
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select = "SELECT *
FROM orders
LEFT JOIN payments ON orders.order_id = payments.orderID
WHERE orders.created_at < DATE_SUB( CURDATE( ) , INTERVAL 1 HOUR )
AND payments.processed < 1";
$conn->exec($select);
if($select > 0)
// sql to delete a record
$sql = "delete from `orders` where ...)";
$conn->exec($sql);
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>
显然,如果条件部分删除,我的构造有问题。你能帮我一点吗?