php - 更新MySQL表的特定记录

标签 php mysql

我正在处理电话系统,必须与多个服务供应商合作。对于一个供应商,我有一个像这样的 MySQL 表 country_codes -

---------------------------------------------------------
country_code  |  area_code  |  country
---------------------------------------------------------
93            |  93         |  Afghanistan
0             |  9375       |  Afghanistan Cellular-AT
0             |  9370       |  Afghanistan Cellular-AWCC
355           |  355        |  Albania
0             |  35568      |  Albania Cellular-AMC
0             |  35567      |  Albania Cellular-Eagle
213           |  213        |  Algeria
0             |  21377      |  Algeria Cellular-Djezzy
0             |  2135       |  Algeria Cellular-Wataniya
---------------------------------------------------------

等等……

country_code 列以前不存在,但我添加了它,因为我的 PHP 应用程序需要它。我设法更新了一些记录的国家代码(使用我之前在此处提出的问题的答案)

我想要实现的是用各自的国家代码替换 0。所以表格应该是这样的 -

---------------------------------------------------------
country_code  |  area_code  |  country
---------------------------------------------------------
93            |  93         |  Afghanistan
93            |  9375       |  Afghanistan Cellular-AT
93            |  9370       |  Afghanistan Cellular-AWCC
355           |  355        |  Albania
355           |  35568      |  Albania Cellular-AMC
355           |  35567      |  Albania Cellular-Eagle
213           |  213        |  Algeria
213           |  21377      |  Algeria Cellular-Djezzy
213           |  2135       |  Algeria Cellular-Wataniya
---------------------------------------------------------

我希望我对自己的解释足够好。知道如何使用 PHP-MySQL 做到这一点吗?

(我不介意使用 PHP 代码以这种方式操作表格)

最佳答案

试试这个查询-

UPDATE country_codes
SET country_code := @c := IF(@c IS NOT NULL AND country_code = 0, @c, country_code)
ORDER BY CAST(area_code AS CHAR)

关于php - 更新MySQL表的特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9597428/

相关文章:

php - 以编程方式检查网站重定向

php - 在图像悬停和更新页面上查询 MySql DB 而不刷新

php - 使用 $wpdb->prepare 查询 IN 子句

php - 阻止用户提交表单

php - 如何在 laravel dompdf 中自定义字体和页面?

C# - 无法更新 mysql 数据库列值

mysql - 拉维尔 : Migration with Foreign Key

php - 在此查询中添加多对多表关系

mysql - 从另一个表插入后更新行

mysql - 如果 COUNT(*) > X 则分组,否则不分组