SQL - Updating rows based on help data in a lookup table

I have a tbl_english table containing the column "word". I also have a table, tbl_translation, containing the columns "german_word" and "english_word".

tbl_translation is a lookup table to see if there are any Germanic words in the tbl_english "word" column

So what I want to do

For each tbl_english.word, iterate through tbl_translation.german_word and find the corresponding value. If the match exists, update tbl_english.word with the value in tbl_translation.english_word from the current line in tbl_translation

The goal is to replace any exiled German words existing in tbl_english with their correct translation from the tbl_translation lookup table

So far I have come up with the following:

UPDATE tbl_english SET word = (SELECT english_word FROM tbl_translation WHERE tbl_english.word = german_word) WHERE word IN (SELECT german_word FROM tbl_translation WHERE tbl_english.word = german_word ) 

However, this does not succeed when there are several instances of the same or different words obtained as a result of the first sub-selection. Is there an easy way to solve the problem?

Example:

tbl_english contains; Mädchen Frau Boy Giraffe Baum

tbl_translation contains (German, English); Mädchen, Female Frau, Woman

So, in tbl_english, I would like to see the following result; female female boy Giraffe Baum

Edit: not every word in tbl_english will have a reference string in the translation table. Edit2: Added example

+4
source share
2 answers
 UPDATE e SET word = t.english_word FROM dbo.tbl_english AS e INNER JOIN dbo.tbl_translation AS t ON e.word = t.german_word WHERE e.word <> t.english_word; 
+7
source

Have you tried using aliases for subframes?

 UPDATE tbl_english SET word = (SELECT TOP 1 t.english_word FROM tbl_translation t WHERE tbl_english.word = t.german_word) WHERE word IN (SELECT TOP 1 t.german_word FROM tbl_translation t WHERE tbl_english.word = t.german_word) 

I hope I do not understand your question.

+1
source

Source: https://habr.com/ru/post/1399064/


All Articles