SQLtp3.2批量更新saveAll
/**
* 批量更新数据
* @param [array] $datas [更新数据]
* @param [string] $table_name [表名]
*/
public function saveAll($datas,$table_name,$pk){
$sql = ''; //Sql
$lists = []; //记录集$lists
foreach ($datas as $data) {
foreach ($data as $key=>$value) {
if($pk===$key){
$ids[]=$value;批量更新sql语句
}else{
$lists[$key].= sprintf("WHEN %u THEN '%s' ",$data[$pk],$value);
}
}
}
foreach ($lists as $key => $value) {
$sql.= sprintf("`%s` = CASE `%s` %s END,",$key,$pk,$value);
}
$sql = sprintf('UPDATE __%s__ SET %s WHERE %s IN ( %s )',strtoupper($table_name),rtrim($sql,','),$pk,implode(',',$ids)); return M()->execute($sql);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。