Database Translation: A Step-by-Step Guide to Retrieving Translations from One Database Using Another
As a database administrator or developer, you often find yourself dealing with translations of data. When working with multiple databases, it can be challenging to translate words or phrases from one language to another. In this article, we will explore how to translate words from one database using the translation in another database.
Understanding the Problem and Data Structure
Let’s take a look at an example of two databases:
Database 1: Images
| id | description | tags | imagename | resolution | location |
|---|---|---|---|---|---|
| 1 | We standing at eiffeltower | france, green, blue | IMG01.JPG | 1280x1024 | /img/2020 |
| 2 | We standing at bridge | france, orange, grey | IMG02.JPG | 1280x1024 | /img/2020 |
Database 2: Tag Translations
| id | tag | translation |
|---|---|---|
| 1 | france | frankrijk |
| 2 | orange | oranje. |
| 3 | grey | grijs. |
| 4 | green | groen |
| 5 | blue | blauw |
We want to retrieve the translated values for each image description, along with the original tags and image details.
The Solution: Using a Correlated Subquery
To achieve this, we can use a correlated subquery that retrieves rows from the translation table whose tag can be found in the tags list of the corresponding images row. We will use MySQL’s find_in_set() function to find matching tags and the group_concat() function to regenerate a delimited list of translations.
MySQL Query
SELECT
i.id,
i.description,
(
SELECT group_concat(
tt.translation
ORDER BY FIND_IN_SET(tt.tag, REPLACE(i.tags, ', ', ','))
Separator ', '
)
FROM tagTranslations tt
WHERE FIND_IN_SET(tt.tag, REPLACE(i.tags, ', ', ','))
) AS tags,
i.imagename,
i.resolution,
i.location
FROM images i;
How It Works
- The outer query selects the image ID, description, and other relevant details.
- The correlated subquery uses
find_in_set()to find rows from the translation table whosetagmatches any tag in theimagesrow’stagscolumn. We useREPLACE(i.tags, ', ', ',')to remove spaces after commas for the function to work properly. - The aggregation function
group_concat()regenerates a delimited list of translations in the order of matching tags. - The subquery uses an outer join to retrieve the translation details.
Demo on DB Fiddle
You can test this query using the example data provided in the question.
| id | description | tags | imagename | resolution | location |
|---|---|---|---|---|---|
| 1 | We standing at eiffeltower | frankrijk, groen, blauw | IMG01.JPG | 1280x1024 | /img/2020 |
| 2 | We standing at bridge | frankrijk, oranje, grijs | IMG02.JPG | 1280x1024 | /img/2020 |
Conclusion
Translating words from one database using another can be a complex task. By understanding the data structure and using MySQL’s find_in_set() and group_concat() functions, we can create an efficient correlated subquery to achieve this translation.
Remember to always normalize your data by storing delimited lists in separate tables, as shown in the example above. This approach helps prevent issues with data consistency and accuracy.
With this solution, you should now be able to translate words from one database using another, making it easier to manage multilingual content across different databases.
Last modified on 2025-02-06