php控制 sql语句 插入大量数据

sql插入大量数据
第一种 使用循环一条一条插入

    foreach($list as $key=>&$value){
        if($key != 0){
            $insertidSql .= ',';
        }

        $insertidSql .= '("'.$uid.'","'.$new_id.'")';

        $result = query('INSERT INTO detail (uid,new_id,) VALUES '.$insertidSql.';');
    }

第二种 插入 一条

    foreach ($base_arrey as $key => $value) {

        // 一条一次
        $data['type'] = '1';
        $data['id'] = $value['id'];

        $a->add($data); 

    }

第三种 一次插入多条

    $count_land = count($base_arrey);
    foreach ($base_arrey as $key => $value) {

        if($sql_data != ''){
            $sql_data .= ',';
        }
        $sql_data .= '("1","'.$value['id'].'")';

        if($key != 0 && $key % 100 == 0 || $key == $count_land - 1){

            $insert_sql = "INSERT INTO table_name (id,type) VALUES " . $sql_data;

            $result = query($insert_sql);

            $sql_data = '';

        }
    }

猜你喜欢

转载自blog.csdn.net/appAndWxy/article/details/82150781