[英]copy data from one table to another using foreign key
i am facing a problem regarding sql query. 我面临有关sql查询的问题。
i have a table structure 我有一个表结构
A_id : 10,20,30,40
A_name: a,b,c,d
B_id: null,null,null,null
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 桌子看起来像
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.