Mysql: joining tables for translation records
I have 2 tables with this configuration:
table language('id', 'language_name', 'iso_code')
table translation('id', 'language_id', 'translated_text')
In the first table I have records:
---------------------------------
| id | language_name | iso_code |
---------------------------------
| 1 | English | en |
| 2 | Espanõl | es |
| 3 | Français | fr |
---------------------------------
The second table:
--------------------------------------
| id | language_id | translated_text |
--------------------------------------
| 1 | 1 | Good Morning |
| 2 | 1 | How are you? |
| 1 | 2 | Buenos dias |
| 2 | 3 | Comment ça va? |
--------------------------------------
All English text strings exist, but some of the other languages dont.
I would like to show a table with ALL English text strings and corresponding translations, like:
----------------------------------------
| text_id | en | es |
----------------------------------------
| 1 | Good Morning | Buenos dias |
| 2 | How are you? | |
----------------------------------------
or
-------------------------------------------
| text_id | en | fr |
-------------------------------------------
| 1 | Good Morning | Comment ça va? |
| 2 开发者_开发百科 | How are you? | |
-------------------------------------------
Any ideas?
Just keep doing left joins to same table on the ID, but extra columns representing their language...
Edited to show English if no value in corresponding columns per comment inquiry.
select
eng.id,
eng.translated_text InEnglish,
coalesce( spn.translated_text, eng.translated_text ) InSpanish,
coalesce( frn.translated_text, eng.translated_text ) InFrench
from
translation eng
left join translation spn
on eng.id = spn.id
and spn.Language_ID = 2
left join translation frn
on eng.id = frn.id
and spn.Language_ID = 3
where
eng.Language_id = 1
order by
eng.id
精彩评论