@JW 和我或多或少同时到达以下位置,但 JW 准时获得优势!
UPDATE citytable a
INNER JOIN codetable b ON a.cityname LIKE CONCAT('%',b.city,'%')
SET a.codenumber = b.codenumber
在 Morgan 要求在 SELECT 中执行此操作的 php 示例后更新,然后使用 UPDATE 循环。
<?php
#Fill out the four variables below.
#
#This is just an example!
#If you are going to use this for real, you want to put the top
#4 variables in a separate file and include that file into this
#file via phps include directive. That separate file needs
#to be in a tightly security controlled directory, because
#your database password is in the file.
#
#For security reasons, the variables below must not come from
#user supplied data (from a POST or GET or the SESSION variables).
#
$dbName = '';
$hostName = '';
$username = '';
$password = '';
$dbh = new PDO("mysql:dbname=$dbName;host=$hostName",
$username, $password);
$dbh->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
$sqlSelect = "
SELECT cityname, codenumber
FROM city a
INNER JOIN codetable b ON
a.cityname LIKE CONCAT('%',b.city,'%');";
$sqlUpdate = "
UPDATE citytable SET codenumber = ? WHERE cityname = ?";
$rows = $dbh->query($sqlSelect)->fetchAll();
$sth = $dbh->prepare($sqlUpdate);
foreach($rows as $row) {
$codeNumber = $row['codenumber'];
$cityName = $row['cityname'];
$sth->execute(array($codeNumber,$cityName));
}