2

都市名テーブル(citytable)を持つテーブルが1つあります

idcity  |   cityname     |  statename  |    codenumber
   1    |   Los Angeles  |   state2    |     ...
   2    |   New York     |   state3    |     ...
   3    |   New Jersey   |   state3    |     ...

市区町村表(codetable)

  id |  city     | codenumber
   1 |  angeles  |   031
   2 |  york     |   064
   3 |  jersey   |   075

どうやってSET or INSERT or UPDATE data 'codenumber' FROM 'codetable' fields INTO 'codenumber' column FROM citytable WHERE 'city' FROM codetable LIKE '%cityname%' FROM 'citytable'?事前にご協力いただきありがとうございます。

4

2 に答える 2

2

使用するUPDATE with join

UPDATE  cityTable a
        INNER JOIN codeTable b
            ON a.ID = b.ID
SET     a.codeNumber = b.codeNumber

しかし、私はここにあると思いIDますAUTO_INCREMENTed コラム, もしそうなら,

UPDATE  cityTable a
        INNER JOIN codeTable b
            ON a.cityName LIKE CONCAT('%', b.city,'%')
SET     a.codeNumber = b.codeNumber
于 2013-01-18T16:39:57.370 に答える
0

@JW と私はほぼ同時に以下に到達しましたが、JW は時間通りに優位に立ちました!

UPDATE citytable a
INNER JOIN codetable b ON  a.cityname LIKE CONCAT('%',b.city,'%')
SET a.codenumber = b.codenumber

Morgan がこれを SELECT で実行し、その後に UPDATE でループする PHP の例を求めた後、更新してください。

<?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));

}
于 2013-01-18T16:42:59.843 に答える