2

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?

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
user2660234
  • 67
  • 2
  • 8

2 Answers2

2

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'
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

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')
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83