简体   繁体   English

使用外键将数据从一个表复制到另一个表

[英]copy data from one table to another using foreign key

i am facing a problem regarding sql query. 我面临有关sql查询的问题。

i have a table structure 我有一个表结构

TABLE_A TABLE_A

A_id : 10,20,30,40 
A_name: a,b,c,d  
B_id: null,null,null,null

TABLE_B TABLE_B

B_id: 1,2,3,4  
B_name:n1,n2,n3,n4 
A_id: 10,20,30

Now I need to copy the B_id from TABLE_A to B_id of TABLE_B where A_id of TABLE_A is same as A_id of TABLE_B 现在, 我需要将B_id从TABLE_A复制到TABLE_B的B_id,其中TABLE_A的A_id与TABLE_B的A_id相同

the table will then look like 桌子看起来像

TABLE_A TABLE_A

A_id : 10,20,30,40  
A_name: a,b,c,d   
B_id: 1,2,3,null

I have tried this: 我已经试过了:

UPDATE TABLE_A 
SET B_id = a.B_id
from TABLE_A a 
WHERE TABLE_A.A_id = a.A_id

but its not working and the error is SQLCODE=-104, SQLSTATE=42601 但它不起作用,错误是SQLCODE=-104, SQLSTATE=42601

Have you tried this 你试过这个了吗

UPDATE TABLE_A SET B_id = b.B_id 
from TABLE_B b 
WHERE TABLE_A.A_id = b.A_id
update TABLE_A A,TABLE_b B set A.b_id = B.b_id where A.a_id = B.a_id;

If you are working on mysql . 如果您正在使用mysql Don't know other database will accept the syntax or not. 不知道其他数据库是否接受语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当外键仅在目标表中定义时,如何将数据从一个表复制到另一个表? - How to copy data from one table to another when foreign key is only defined in the destination table? 从一个表复制到另一个包含额外列外键的表 - Copy from one table to another table containing an extra column foreign key 将一个表的外键约束复制到mysql中同一个数据库中的另一个 - copy the foreign key constraints of one table to another in the same db in mysql SQL:使用两个表中都存在的唯一键将数据从一个表复制到另一个表 - SQL: Copy data from one table into another using unique key present in both tables 如何将与外键对应的值从另一个表复制到该表? - How to copy values corresponding to the foreign key from another table to this table? INSERT INTO 使用外键和另一个表中的数据 - INSERT INTO with foreign key and data from another table 从另一个表插入数据作为外键 - Insert data from another table as a foreign key 使用SQL Server将数据从一个表复制到另一表 - Copy data from one table to another table using sql server 使用sql将数据从一个表复制到另一个现有表 - copy data from one table to another existing table using sql 如何将数据插入表中,其中一个值应该是另一个表的外键? - How do I insert data into a table where one of the values should be a foreign key from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM