$sql1 = 'insert into business_ip (gid, name, area, belongName, belongArea, destIPv4, created, updated) values ';
$sql2 = 'on duplicate key update name = values(name), area = values(area), belongName = values(belongName), belongArea = values(belongArea), destIPv4 = values(destIPv4), created = values(created), updated = values(updated)';
$values = ''; foreach ($data as $v) {
$values .= '('.$v['gid'].', "'.$v['name'].'", "'.$v['area'].'", "'.$v['belongName'].'", "'.$v['belongArea'].'", "'
.$v['ip'].'", "'.$v['created'].'", "'.$v['updated'].'"),';
}
$query = $sql1.substr($values,0,-1).$sql2.';';
Yii::$app->db->createCommand($query)->execute();
主要就是运用mysql的 on dumpicate key update
字段中需有unique索引的字段.
结合yii2 $db->batch()方法可大量处理数据,记录一下.
05-13 15:54