开发者

Translation query

I have a MySQL table with translations. Now I am searching for a query that returns me the first translation matching a given list of languages:

Lets say there are 4 translations of a string available:

id          lang     translation
------------------------开发者_开发问答--------
greeting    en       hey!
greeting    de       Hallo.
greeting    de_at    Servus!
greeting    de_ch    Grüezi

Desired result of the query depending on a given language list:

language list    result
-----------------------
de,en            Hallo.
en,de            hey!
de_at,de,en      Servus!

Is this possible in a single query?


SELECT COALESCE( de_at.translation, de.translation, en.translation )
  FROM (SELECT 'greeting' AS id) AS id
  LEFT JOIN translations AS de_at ON( de_at.lang = 'de_at' AND id.id = de_at.id )
  LEFT JOIN translations AS de ON( de.lang = 'de' AND id.id = de.id )
  LEFT JOIN translations AS en ON( en.lang = 'en' AND id.id = en.id )

I should credit Eddy for this technique.


Try this:

SELECT * FROM translations t WHERE id = 'greeting' AND lang IN ('de_at', 'de', 'en') ORDER BY FIELD(lang, 'de_at', 'de', 'en') LIMIT 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜