MySQL多条分批插入

  • A+
所属分类:MySQL

1.字符串拼接方法

$sql = $sql_pre = "INSERT INTO term_pv_rev (TermId,impv,rev) VALUES ";
$i = 0;
foreach ($term as $key => $value)
{
    $i++;
    $sql .= "("'.$key.'","'.$value['impv'].'","'.$value['rev'].'"), ";
    if($i % 200 == 0)
    {
        $objMysql->query(substr($sql,0,-1));
        $sql = $sql_pre;
    }    
}
if($sql != $sql_pre)
{
    $objMysql->query(substr($sql,0,-1));
}

2.数组方法

$sql = $sql_pre = "INSERT INTO term_pv_rev (TermId,impv,rev) VALUES ";
$insert_arr = array();
foreach ($term as $key => $value) 
{
$insert_arr[] = "("'.$key.'","'.$value['impv'].'","'.$value['rev'].'")";
if(sizeof($insert_arr)>=200)
{
$sql = $sql.implode(',', $insert_arr);
$objMysql->query($sql);
$sql = $sql_pre;
$insert_arr = array();
}
}
if($insert_arr)
{
$sql = $sql.implode(',', $insert_arr);
$objMysql->query($sql);
}
  • 免费领红包
  • 支付宝红包扫一扫
  • weinxin
  • 免费提现
  • 支付宝全额提现
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: