[英]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.