社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

Mysql:如何将表字段更新为从另一个表获取的值[重复]

Paras • 5 年前 • 1790 次点击  

我有两张桌子 A和B 具有以下字段:

表A

| ID | COUNTRY_CODE | COUNTRY_NAME | FIRST_NAME | LAST_NAME |

表B

| ID | COUNTRY_CODE | COUNTRY_NAME |

现在我需要更新 国家代码 字段来自 表A 要从中获取其值的 表B .

伪代码如下:

for all rows in Table A :
  set A.country_code = (select B.country_code from B where B.country_name = A.country_name
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51435
文章 [ 2 ]  |  最新文章 5 年前
Shawn.X
Reply   •   1 楼
Shawn.X    6 年前

不需要 join ,只需按以下方式尝试sql:

update TableA set country_code = B.country_code from TableB B where A.country_name = B.country_name 
Fahmi
Reply   •   2 楼
Fahmi    6 年前

使用更新 JOIN

update TableA A
inner join tableB B on B.country_name = A.country_name
set A.country_code=B.Country_code