如何避免循环中的查询

时间:2015-05-11 09:19:44

标签: php mysql pdo

您好我怎么能避免这样的查询?

示例查询:

$sql = DB::getInstance()->prepare("SELECT tb_id1 FROM table1 WHERE duedate < ?");
$sql->execute(array($currentdate));
  if($sql->rowCount()){
     while($row = $sql->fetch(PDO::FETCH_ASSOC)){      

   $sql2 = DB::getInstance()->prepare("UPDATE table2 SET isOverdue = 1 WHERE tb_id2 = ?");
   $sql2->execute(array($row["tb_id1"]));


    }
 }

1 个答案:

答案 0 :(得分:1)

您可以使用update with join,因此不使用PHP中的任何循环

update table2 t2
join table1 t1 on t1.tb_id1 = t2.tb_id2
set t2.isOverdue = 1 
where 
t1.duedate < ?