I have a database with this structure
table_translations
| name | cat | text_en | text_es | text_pt | text_fr |
|---|---|---|---|---|---|
| item1 | 01 | one | uno | un | un |
| item1 | 02 | one_B | uno_b | un_b | |
| item2 | 01 | two | dos | dois | deux |
| item3 | 01 | one | uno | ||
| item4 | 01 | four | quatre |
table1_items
| name | cat | column1 | column2 | column3 |
|---|---|---|---|---|
| item1 | 01 | c-1 | c-2 | c-3 |
| item3 | 01 | c-1 | c-2 | c-3 |
table2_items
| name | cat | column1 | column2 | column3 |
|---|---|---|---|---|
| item1 | 01 | c-1 | c-2 | c-3 |
| item2 | 01 | c-1 | c-2 | c-3 |
With this query I get the translations for each table_items (there are several table_items):
SELECT *
FROM table1_items, table_translations
WHERE table_translations.name = table1_items.name
AND table_translations.cat = table1_items.cat;
| name | cat | column1 | column2 | column3 | text_en | text_es | text_pt | text_fr |
|---|---|---|---|---|---|---|---|---|
| item1 | 01 | c-1 | c-2 | c-3 | one | uno | un | un |
| item3 | 01 | c-1 | c-2 | c-3 | one | uno |
However, I would like check if the translation_"language" is empty, then select the value from translation_en. Is that possible?
| name | cat | column1 | column2 | column3 | text_en | text_es | text_pt | text_fr |
|---|---|---|---|---|---|---|---|---|
| item1 | 01 | c-1 | c-2 | c-3 | one | uno | un | un |
| item3 | 01 | c-1 | c-2 | c-3 | one | uno | one | one |
JOINsyntax. Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed!COALESCE(text_es, text_en), COALESCE(text_pt, text_en), COALESCE(text_fr,text_en)NULLor empty string? If it's the firstCOALESCEis your friend. But that would require you explicitly name the columns you want to return. Ie instead ofselect *useselect ..., coalesce(text_es, text_en)...