MYSQL - 将数据从一个表移动到一个相关的表? [英] MYSQL - Move data from one table to a related one?

查看:49
本文介绍了MYSQL - 将数据从一个表移动到一个相关的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表格,结构如下:

I have two tables, structured like so:

table A:
   A_ID    varchar(32) PRIMARY KEY
   field1  varchar(255)
   field2  varchar(255)
   B_ID    varchar(32)

table B:
   B_ID    varchar(32) PRIMARY KEY
   field1  varchar(255)
   field2  varchar(255)

A 包含表 B 的外键(请注意,1 B 可以有 1 个以上的 A).我想将表 B 中的数据插入到它匹配的表 A(当前表 A 中的每一行的 field1 和 field2 都是空的).有没有办法完全用 MySQL 来做到这一点?

A contains a foreign key to table B (note that 1 B could have more than 1 A). I'd like to insert the data from table B to it's matching table A (field1 and field2 is empty for every row in table A currently). Is there a way to do this purely with MySQL?

推荐答案

尝试

UPDATE `table_a` AS a 
INNER JOIN `table_b` AS b ON ( a.`b_id` = b.`id` ) 
SET a.`field1` = b.`field1`, a.`field2` = b.`field2`

这篇关于MYSQL - 将数据从一个表移动到一个相关的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆