Question

I have a query as like

SELECT * 
FROM aTable
LEFT JOIN aTableTranslate
ON aTable.id = aTableTranslate.aTable_id
WHERE
 aTableTransalte.language like 'en'

question is ....

is there any way to filter as like

...

WHERE
 aTableTranslate.language like (IF EXIST_A_FIELD_FOR 'en' THEN 'en' ELSE IF EXIST_A_FIELD_FOR 'jp' THEN 'jp' OR 'cn')

?

I want to show list with 1. visitor's language > 2. english ... > or default language.

is it possible by query?

Was it helpful?

Solution

You could join to translation tables separately, and pick the first non-NULL translation with COALESCE, like this:

SELECT a.*, COALESCE(t1.translation, t2.translation, t3.translation) as translation
FROM aTable a
LEFT JOIN aTableTranslate t1 ON aTable.id = t1.aTable_id AND t1.language like 'en'
LEFT JOIN aTableTranslate t2 ON aTable.id = t2.aTable_id AND t2.language like 'jp'
LEFT JOIN aTableTranslate t3 ON aTable.id = t3.aTable_id AND t3.language like 'cn'

OTHER TIPS

Try this:

SELECT a.*, COALESCE(IF(t1.language = 'en', t1.translation, NULL), 
                     IF(t1.language = 'jp', t1.translation, NULL), 
                     IF(t1.language = 'cn', t1.translation, NULL)
                   ) AS translation
FROM aTable a
LEFT JOIN aTableTranslate t1 ON aTable.id = t1.aTable_id AND t1.language IN ('en', 'jp', 'cn')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top