|
|
发表于 2019-12-10 15:16:08
|
显示全部楼层
[ol] $mysqli = mysqli_connect('localhost', '数据库用户名', '数据库密码', '数据库名字'); $sql = 'select * from `yunfile`'; $res = mysqli_query($mysqli, $sql); $uniq_key_val = array(); while ($row = mysqli_fetch_assoc($res)) { $uniq_key_val[$row['yunfile']] = $row['id']; } $file = "CharList.txt"; //将excel文件导出成txt格式 后的文件名字 $con = file($file); $sql = "INSERT INTO `yunfile` ( `yunfile`,`softwriter`) VALUES "; array_shift($con);//如果excel文件第一行 是 行名字 则保留这行,否则 在这行前面加 // 屏蔽这行,或者删除这行; array_pop($con); $sqlUpdate = "UPDATE `yunfile` a JOIN ( "; foreach ($con as $key => $value) { $valueArr = explode(" ",$value); $sql_chip[] = "SELECT '" . $uniq_key_val[$valueArr[0]] . "' AS `id`, '" . trim($valueArr[0]) . "' AS `yunfile` , " . """ . trim($valueArr[1]) . "" AS `softwriter`"; } $sqlUpdate .= implode(" UNION ", $sql_chip) . " ) b USING (`id`, `yunfile`) SET a.`softwriter` = b.`softwriter`;"; mysqli_query($mysqli,$sqlUpdate); mysqli_close($mysqli);[/ol]复制代码
1) 本程序是php版,在php环境下执行;
2) 数据库表中如果没有自增ID字段,请添加此字段,int 类型,AUTO_INCREMENT;
3) 把保存对应值的excel文件另存,导出成txt格式文件,该文件放的位置和本程序同目录;
4) 把你服务器上数据库的连接用户名、密码、数据库名字填写在程序第一行的对应位置;
5) 本程序假设存放表名字为yunfile,唯一标示码的字段是:yunfile,需要变更的值存放字段为:softwriter,如果有不同,则依照此规则把程序中对应的位置;
5) 运行该程序,可以一次性更新文件中所有数值;
|
|