将单表的数据写入到新表的脚本

本文介绍了一段PHP脚本,该脚本用于从一个数据库表中读取数据,并将其迁移到多个目标表中。通过循环及SQL查询实现了数据的有效迁移,适用于需要进行大量数据迁移的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
require_once(dirname(dirname(__FILE__)) . '/inc/cron.header.php');
$db_other = new common_db('caiyun_v02_other');


$total = 0;
$index = 0;
$limit = 5000;
$stime = time();
while (TRUE) {

    $sql = "select * from shop_user_score_log_4 where id > 5912498 order by id asc limit $index,$limit";//最后一次跑的时候

  $sql = "select * from shop_user_score_log_4   order by id asc limit $index,$limit"//第一次跑的时候

    $score_list = $db_other->execute($sql);


    if (empty($score_list)) {
        echo '没有数据了\n\r';
        break;
    }


    foreach ($score_list as $value) {
        $id = $value['id'];
        $lender_id = $value['lender_id'];
        $table_name =  $table_name = 'shop_user_score_logs_'.$lender_id%20;
        echo 'shop_user_score_log_3 log_id='.$id.'%%%lender_id ='.$lender_id.'%%%table_name='.$table_name."\n\r";
        $sql = "insert into {$table_name} (lender_id,oprate_type,action,relation_id,state,score,pre_score,post_score,day,create_time) "
        . "values('$lender_id','{$value['oprate_type']}','{$value['action']}','{$value['relation_id']}','{$value['state']}','{$value['score']}','{$value['pre_score']}','{$value['post_score']}','{$value['day']}','{$value['create_time']}')";
        $db_other->execute($sql);
        $total+=1;
    }
    $index += $limit;
}
$total_time = time()-$stime;


echo '一共发送' . $total . '条数据最后ID是('.$id.')共计时间:'.$total_time;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值