繁体   English   中英

使用来自另一个数据库表的数据更新(填充)SQL表列

[英]Update(populate) SQL Table column with data from another database table

我有两个数据库, test1test2 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

我想将数据库test2table2的单词定义复制到数据库test1table1 ,其中table1中的单词与table2的相同单词匹配,并避免重复的单词; 例如,单词abandontable2出现两次,而在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 1fetch first 1 row only

暂无
暂无

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

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