MySQL输出CSV并从CSV更新


MySQL output CSV and update from CSV

下面有一个查询,我从数据库中选择一个ID,然后用1更新该ID的字段,以指示该记录已被处理。我现在需要执行相同的过程,但选择50个ID并以CSV格式输出它们,然后再次用1更新每个记录,以指示这些记录已被处理。感谢您的帮助,我不确定最有效的方法。

$result = mysql_query("SELECT `id` FROM `t_ids` WHERE `f_fetched` IS null LIMIT 1");
while($row = mysql_fetch_array($result))
$f_id = $row['id'];
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE id = '$f_id'");

您可以使用类似的东西

$result = mysql_query("SELECT `id` FROM `t_ids` WHERE `f_fetched` IS null LIMIT 50");
$processed_ids = array();
while($row = mysql_fetch_array($result))
{
    //do whatever processing you need to with that id
    //add the id to the $processed_ids array
    $processed_ids[] = $row['id'];
}
$ids = implode(",", $processed_ids);  //create a comma-delimited string of ids.
//update all rows in 1 query
mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE id IN ($ids)");  

为什么不直接使用:

mysql_query("UPDATE t_ids SET f_fetched = '1' WHERE  `f_fetched` IS null");