如何在某些列内容与另一个表列内容类似的情况下更新MySQL设置值

如何在某些列内容与另一个表列内容类似的情况下更新MySQL设置值,mysql,insert-update,sql-like,Mysql,Insert Update,Sql Like,我有一张桌子 城市名称表(城市表) 代码编号城市表(代码表) 如何将“codetable”字段中的数据“codenumber”设置或插入或更新到citytable中的“codenumber”列中,其中代码表中的“city”类似于“citytable”中的“%cityname%”? 感谢您的帮助。使用updatewithjoin UPDATE cityTable a INNER JOIN codeTable b ON a.ID = b.ID SET

我有一张桌子 城市名称表(城市表)

代码编号城市表(代码表)

如何将“codetable”字段中的数据“codenumber”设置或插入或更新到citytable中的“codenumber”列中,其中代码表中的“city”类似于“citytable”中的“%cityname%”?
感谢您的帮助。

使用
updatewithjoin

UPDATE  cityTable a
        INNER JOIN codeTable b
            ON a.ID = b.ID
SET     a.codeNumber = b.codeNumber
但我怀疑
ID
AUTO_INCREMENT
ed列,如果是

UPDATE  cityTable a
        INNER JOIN codeTable b
            ON a.cityName LIKE CONCAT('%', b.city,'%')
SET     a.codeNumber = b.codeNumber

@JW和我差不多同时到达了以下地点,但JW在时间上占了优势

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

Morgan要求提供一个php示例,说明如何在SELECT中执行此操作,然后使用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).
#

如果所涉及的表很大,那么它的性能就不会很好,因为它的连接不是最优的。但是,如果您需要这样做一次或偶尔,它也会起作用。此外,如果您的表没有不区分大小写的排序规则,您可以将a.cityname包装在LOWER()中,也可以将b.city包装在LOWER()中:
UPDATE citytable a INNER JOIN codetable b ON LOWER(a.cityname),比如CONCAT('%',LOWER(b.city),'%')SET a.codennumber=b.codenumber;“;
我更新了代码示例,因为您询问如何在循环中先选择然后调用UPDATE。看一看!是的,我通常使用它。但是有任何结果(代码号)都是空的(当将citytable与citytable连接时,因为任何城市都没有区号)。我想使用循环代码(PHP)来检查数据(不是空的)然后更新任何记录或再次在循环语句中打印它们,以使组合框中的选项替换电话号码(区号)输入文本(html)中的代码用于使用Javascript代码进行表单注册。您是否看到我更新的循环示例。我认为您应该能够接受该示例并添加空检查?您好,我看到您是新加入的。如果您觉得有答案解决了问题,请单击白色复选标记将其标记为“已接受”(大约15分钟到一小时后)。这有助于将重点放在仍然没有答案的较老的SO问题上。以下是一些链接:,您可以在internet上找到许多来源
:D
我知道如何使用常用语法MySQL“更新集值”“在PHP中。但我想要的是在专业代码中使用嵌套循环PHP代码时。示例在几个步骤中创建代码。首先选择数据(WHERE语句),然后在第一步中有匹配的数据时更新数据。但是如何在PHP代码中使用它呢?我知道如何在PHP中使用常用语法MySQL“更新集值”。但我想要的是在专业代码中使用嵌套循环PHP代码时。示例在几个步骤中创建代码。首先选择数据(WHERE语句),然后在第一步中有匹配的数据时更新数据。但任何结果(代码号)都是空的(当将citytable与citytable连接时,因为任何城市都没有区号)。我想让循环代码(PHP)检查数据(非空),然后更新任何记录或再次在循环语句中打印它们,以在组合框中生成选项,用Javascript代码替换输入文本(html)中的电话号码(区号)中的代码号,以便进行表单注册
UPDATE citytable a
INNER JOIN codetable b ON  a.cityname LIKE CONCAT('%',b.city,'%')
SET a.codenumber = b.codenumber
<?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).
#