[英]Update(populate) SQL Table column with data from another database table
我有两个数据库, test1
和test2
。 test1
具有下表table1
。
ID | Word | Def
1. abandon NULL
2. test NULL
对于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
我想将数据库test2
, table2
的单词定义复制到数据库test1
, table1
,其中table1中的单词与table2
的相同单词匹配,并避免重复的单词; 例如,单词abandon
在table2
出现两次,而在table1
仅出现一次。 如果可能的话,我想将abandon
一词的第二个定义附加到第一个定义之后,并在table1
Def
列中对其进行更新。
我尝试了以下方法。
UPDATE test1.table1
SET Def = (SELECT Def
FROM test2.table2
WHERE test1.table1.Word = test2.table2.Word);
但是我遇到了一个我不太理解的错误。
您只需要获得一个定义。 这是在MySQL和Postgres中有效的方法:
UPDATE test1.table1 t1
SET Def = (SELECT Def
FROM test2.table2 t2
WHERE t1.Word = t2.Word
ORDER BY id
LIMIT 1);
LIMIT 1
也可以select top 1
或fetch first 1 row only
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.