$list = array ();
$query = $this->db->query ( "SELECT id FROM table where cid = 1" );
foreach ( $query->result_array () as $idlist ) {
$list [] = $idlist;
}
$count = count ( $list );
for($i = 0; $i < $count; $i ++) {
$num = $list[$i];
$this->db->query ( "UPDATE category SET zong=XXXX where id =$num " );
}
我是想先查询出所有的id,循环执行更新sql语句,条件where根据上面查询的结果id循环执行
foreach ( $query->result_array () as $idlist ) {
$list [] = $idlist;
}
这里不对
直接写
foreach ( $query->result_array () as $idlist ) {
$this->db->query ( "UPDATE category SET zong=XXXX where id =" + $idlist )
}
代码冗余太多了,记住,永远不要在循环里套sql,效率低到极致了,
因为每次执行$this->db->query 都会消耗连接资源,没有连接池压力更明显,
给你个建议你可以试一下,理论上一条sql就可以了
$this->db->query( "UPDATE category SET zong=XXXX where id in (SELECT id FROM table where cid = 1) ");
楼上简直是胡说数据量很大 你一条sql能拉出来?这不是胡扯吗,单条sql 命中索引比一条sql查所有的快