这是我能想到的唯一方法,我收到了关于限制的错误。我试图在while循环中删除块,因为总结果可能非常大。
第一个计数语句正常。这是第二个删除语句,但没有。我猜是因为连接和/或使用限制。如何在加入时限制删除?
//find total count to delete
$stmt = $db->prepare("
SELECT
COUNT(*)
FROM app_logs
INNER JOIN users
ON users.user_id = app_logs.user_id
INNER JOIN computers
ON computers.computer_id = users.computer_id AND computers.account_id != :account
WHERE app_logs.timestamp < :cutoff_time
");
$binding = array(
'account' => 2,
'cutoff_time' => strtotime('-3 months')
);
$stmt->execute($binding);
//get total results count from above
$found_count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
echo $found_count; //ex. 15324
//delete rows
$stmt = $db->prepare("
DELETE
FROM app_logs
INNER JOIN users
ON users.user_id = spc_app_logs.user_id
INNER JOIN computers
ON computers.computer_id = users.computer_id AND computers.account_id != :account
WHERE app_logs.timestamp < :cutoff_time
LIMIT :limit
");
$binding = array(
'account' => 2,
'cutoff_time' => strtotime('-3 months'),
'limit' => 2000
);
while($found_count > 0)
{
$stmt->execute($binding);
$found_count = $found_count - $binding['limit'];
}
答案 0 :(得分:0)
作为mentioned in the docs和in this answer, LIMIT
不能与DELETE + JOIN
一起使用。
如果您需要以某种方式限制删除,只需为DELETE
语句创建条件即可模拟您的LIMIT
部分。例如,您可以按照以下步骤操作:
DELETE
语句WHERE
AND id <= ?
,并将每个边框ID 代替?
。希望这有帮助。
答案 1 :(得分:-1)
在删除查询中有多个表时,您需要告诉DB要删除哪些表。您可以在第一行
中执行此操作DELETE app_logs
FROM app_logs
INNER JOIN users ON ...
INNER JOIN computers ON ...