简体   繁体   English

从另一个表中的键更新外键

[英]update foreign key from key in another table

I need a query that will take the primary key from one table (table 2) and place it in a second table (table 1) as a foreign key. 我需要一个查询,该查询将从一个表(表2)中获取主键,并将其作为外键放在第二个表(表1)中。 The database is Microsoft Access 2007. I tried the following query but it did not work: 该数据库是Microsoft Access2007。我尝试了以下查询,但没有成功:

update table1 
set table1.table2ID = table2.ID
FROM table1  INNER JOIN  table2 on table1.name = table2.name

The two tables are as follows: 这两个表如下:

Table 1: 表格1:

ID  | table2ID  | Name
--------------------------
1   |           |   Name1   
2   |           |   Name2   
3   |           |   Name1    

Table 2: 表2:

ID    | Name
-----------------
1     |    Name1   
2     |    Name2    

I want the result to be: 我希望结果是:

Table 1: 表格1:

ID  | table2ID  | Name
--------------------------
1   |       1   |   Name1   
2   |       2   |   Name2   
3   |       1   |   Name1    

Try this: 尝试这个:

update table1 
INNER JOIN table2 ON table1.name = table2.name
SET table1.table2ID = table2.ID

or this: 或这个:

update table1, table2
SET table1.table2ID = table2.ID
WHERE table1.name = table2.name

Let me know which one works for you best. 让我知道哪一个最适合您。

U can try this : 你可以试试看

update TABLE1 set tabel2id = TABLE2.Id from TABLE1 s inner join TABLE2 s on u.name = s.NAME 从u.name = s.NAME上的TABLE1的内部联接TABLE2更新TABLE1 set tabel2id = TABLE2.Id

It worked for me.. 它对我有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM