背景:由于地址库的变更,导致历史的用户地址与现用的地址库不对应,比如code变动,归属变动,增删等。
解决思路:如果是地址库存在于mysql里面,数据格式如下:
用户地址表:
地址库表:
则用sql可以查询到异常的地址:
SELECT addr_id,ua FROM ( SELECT substring_index( area, ':',- 1 ) AS ua,addr_id FROM sysuser_user_addrs GROUP by ua ) uadd WHERE ua NOT IN ( SELECT CONCAT_WS( '/', a.region_id, b.region_id, c.region_id ) FROM sdb_ectools_regions a LEFT JOIN sdb_ectools_regions b ON a.region_id = b.p_region_id LEFT JOIN sdb_ectools_regions c ON b.region_id = c.p_region_id )
如果是地址库不存在于mysql,假设是用json存在于前端,则可以用excel的方式来处理
前端地址json如下:
我们用程序导出为cvs
<?php $dir = dirname(__FILE__); //找出当前脚本所在路径 require $dir.'/vendor/autoload.php';//添加读取excel所需的类文件 $objPHPExcel = new PHPExcel(); //实例化一个PHPExcel()对象 $objSheet = $objPHPExcel->getActiveSheet(); //选取当前的sheet对象 $objSheet->setTitle('helen'); //对当前sheet对象命名 //常规方式:利用setCellValue()填充数据 $objSheet->setCellValue("A1","张三")->setCellValue("B1","李四"); //利用setCellValues()填充数据 //取巧模式:利用fromArray()填充数据 $array = array( array("","B1","张三"), array("","B2","李四") ); $data = file_get_contents("./region.json"); $array = json_decode($data,true); $count = 1; foreach ($array as $key=>$val){ foreach ($val['children'] as $k=>$v){ foreach ($v['children'] as $w){ $objSheet->setCellValue("A".$count,$val['id']); $objSheet->setCellValue("B".$count,$v['id']); $objSheet->setCellValue("C".$count,$w['id']); $count++; } } } //print_r($array); //$objSheet->fromArray($array); //利用fromArray()直接一次性填充数据 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); //设定写入excel的类型 $objWriter->save($dir.'/test.xlsx');
然后导出用户的地址:
SELECT substring_index( area, ':',- 1 ) AS ua FROM sysuser_user_addrs GROUP by ua
然后通过excel的vlookup函数进行对比
红色框为异常地址。