簡體   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