• PHP 千万级别数据匹配更新


    <?php

    //示例是数据匹配实例,两张表通过关联字段email匹配mobile

    header("content-type:text/html;charset=utf-8");
    set_time_limit(0);

    $con = mysqli_connect("127.0.0.1","root",'','test');
    if(mysqli_connect_error($con)){
    die("数据库连接错误:".mysqli_connect_error());
    }
    //设置查询编码
    $con->query("set names utf8");
    $sql = "SELECT * FROM `shuju` WHERE mobile<1 ";
    //执行sql
    $result = $con->query($sql);
    //查询所有条数,循环取出条数
    while ($row=$result->fetch_array()) {
    $data[] = $row;
    }
    //初始化参数
    $u = $success=$error = 0 ;
    //循环第一张表的查询结果
    foreach($data as $v){
    $email = trim($v['email']);
    //查询关联条件,此处字段在数据库里应设索引
    $map = "email = '$v['email']";
    //根据关联条件查询第二张表
    $sql2 = "SELECT * FROM `new` WHERE $map";
    $r = mysqli_query($con,$sql2);
    //从数据库中取出单条记录
    $ree = mysqli_fetch_assoc($r);
    if(!empty($ree)){
    $success++;
    //模糊匹配
    // $where = 'email like "%'.$email.'%"';
    //==匹配
    $where = "email = '$email'";
    $sql3 = "UPDATE `shuju` set `mobile`=".$ree['mobile']."WHERE $where";
    $ba = mysqli_query($con,$sql3);
    if($b==true){
    $su++;
    }
    }else{
    $error++;
    }
    }
    echo '成功数:'.$success.'<br/>';
    echo '更新成功数:'.$su.'<br/>';
    echo '找不到匹配的:'.$error.'条';

  • 相关阅读:
    【洛谷 2212】浇地Watering the Fields
    【洛谷 2504】聪明的猴子
    【洛谷 1991】无线通讯网
    【洛谷 3366】最小生成树_Prim
    Spark RDD中两种算子之一:常见Action算子小结
    Spark学习之路Spark之RDD
    Linux系统之pssh系列工具的使用
    MapReduce shuffle过程详解
    批量计算和流式计算
    关于IDEA破解后无法启动问题
  • 原文地址:https://www.cnblogs.com/ayanboke/p/6882979.html
Copyright © 2020-2023  润新知