[英]Update(populate) SQL Table column with data from another database table
I have two databases, test1
and test2
. 我有两个数据库, test1
和test2
。 test1
has the following table1
. test1
具有下表table1
。
ID | Word | Def
1. abandon NULL
2. test NULL
And for test2
database, I have the following table2
. 对于test2
数据库,我有以下table2
。
ID | Word | Def
1. abandon (verb) the meaning of abandon
2. word2 the meaning of word2
3. abandon (noun) the meaning of abandon
I want to copy the definitions of words from the database test2
, table2
to database test1
, table1
where a word from table1 matches with the same word from table2
and avoid duplicate word; 我想将数据库test2
, table2
的单词定义复制到数据库test1
, table1
,其中table1中的单词与table2
的相同单词匹配,并避免重复的单词; for example, the word abandon
appears twice in table2
and only once in table1
. 例如,单词abandon
在table2
出现两次,而在table1
仅出现一次。 If possible, I want to append the second definition of the word abandon
appended to the first definition and update it in table1
, Def
column. 如果可能的话,我想将abandon
一词的第二个定义附加到第一个定义之后,并在table1
Def
列中对其进行更新。
I have tried the following. 我尝试了以下方法。
UPDATE test1.table1
SET Def = (SELECT Def
FROM test2.table2
WHERE test1.table1.Word = test2.table2.Word);
But I am getting an error which I do not quite understand. 但是我遇到了一个我不太理解的错误。
You need to get only one definition. 您只需要获得一个定义。 Here is an approach that works in MySQL and Postgres: 这是在MySQL和Postgres中有效的方法:
UPDATE test1.table1 t1
SET Def = (SELECT Def
FROM test2.table2 t2
WHERE t1.Word = t2.Word
ORDER BY id
LIMIT 1);
The LIMIT 1
might alternatively be select top 1
or fetch first 1 row only
. LIMIT 1
也可以select top 1
或fetch first 1 row only
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.