-3
$db->beginTransaction();
$del = $db->prepare('DELETE FROM hesabat_filter WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$del = $db->prepare('DELETE FROM hesabat_istilik WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$del = $db->prepare('DELETE FROM hesabat_nem WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$del = $db->prepare('DELETE FROM hesabat_rek WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$del = $db->prepare('DELETE FROM hesabat_soyuqluq WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$del = $db->prepare('DELETE FROM hesabat_vent WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$db->commit();

$count = $del->rowCount();
echo $count;

尽管此 PDO 工作正常,但我无法计算所有已删除的行。我认为我应该为每个准备好的查询提供不同的变量,然后收集变量。你的想法是什么?

4

2 回答 2

3
  1. 如果您不使用参数化查询并且不重用查询,则不需要prepare()它。您可以query()按原样进行。
  2. rowCount()只返回最后一个查询的计数。由于这个原因,并且因为你$del一遍又一遍地覆盖,你当然只会得到最后一个查询的计数。您无法执行任何魔术,您只需要rowCount在每次查询后调用并计算总数。
  3. 但是,而不是query+ rowCount,有PDO::exec,它已经完全符合您的要求:

    PDO::exec()在单个函数调用中执行 SQL 语句,返回受语句影响的行数。

    $totals  = $db->exec('DELETE ...');
    $totals += $db->exec('DELETE ...');
    ...
    
    echo $totals;
    
于 2013-11-02T11:59:50.383 回答
1

每次查询执行后递增计数器可变..像这样尝试

$count = 0;
$db->beginTransaction();
$del = $db->prepare('DELETE FROM hesabat_filter WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$count += $del->rowCount();
$del = $db->prepare('DELETE FROM hesabat_istilik WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$count += $del->rowCount();
$del = $db->prepare('DELETE FROM hesabat_nem WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$count += $del->rowCount();
$del = $db->prepare('DELETE FROM hesabat_rek WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$count += $del->rowCount();
$del = $db->prepare('DELETE FROM hesabat_soyuqluq WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$del = $db->prepare('DELETE FROM hesabat_vent WHERE model_id NOT IN (SELECT res_id FROM result)');
$del->execute();
$count += $del->rowCount();
$db->commit();

echo $count;
于 2013-11-02T11:59:01.450 回答