I have a database table, dictionary, that has two strings and a language id.
The columns are:
id
product_id
key
translation
language_id
Another table, dictionary_versions, just has the dictionary_id and version of translation
id
dictionary_id
version_id
I have two different translations in the table for each key, but sometimes, only one translation for the key. I am looking for a way to get all translations of one language, and, if there are any, translation of another language id.
Im looking to get all translation of one language_id and organizer_id and one version. I have tried to create a temporary table with all values of what I am comparing to, then doing a left join of the temporary table on the dictionary table to get all translated values of the desired language, and if there are any translations of another language, include those.
The problem is that it gets slow when there are 10000 translations for two languages. Is there a better way to join a table onto itself using the dictionary_versions table as a where clause on both?
SELECT
d.*
FROM
dictionary d
LEFT JOIN
dictionary_versions dv ON dv.dictionary_id = d.id
LEFT JOIN
dictionary d2
LEFT JOIN
dictionary_versions dv2 ON d2.id = dv2.dictionary_id
ON
d2.key = d.key
WHERE
d.product_id = 1 AND dv.version_id = 3
AND
d.language_id = 1
LIMIT
0,10
This was one of the queries I tried. However, if there are multiple versions, then it gets all version of the fd2 table, leading to data inaccuracies.
The other way I tried, was with the temporary table, this works, but it slow. it is done in two queries:
CREATE TEMPORARY TABLE IF NOT EXISTS dictionary_temp_1
AS (
SELECT d.* FROM `dictionary` AS `d`
LEFT JOIN `dictionary_versions` AS `dv` ON dv.dictionary_id = d.id
WHERE d.product_id = 1 AND dv.version_id = 3 AND d.language_id = 1
ORDER BY fd.key ASC LIMIT 0,10 )
Second:
SELECT
d.key,
d2.key AS toKey,
d.translation AS `1`,
d2.translation AS `2`
FROM
`dictionary` AS `d`
LEFT JOIN
`dictionary_versions` AS `dv` ON d.id = dv.dictionary_id
LEFT JOIN
`dictionary_temp_1` AS `d2` ON d2.key = d.key
WHERE
d.product_id = 1 AND dv.version_id = 3 AND d.language_id = 1
ORDER BY
d.key ASC LIMIT 0,10