I have a database with questions and answers that get translated into all languages that site is used by. But when a question is not translated yet I want to show that question in english language. So a gettext-like behavior.
My current SQL query for this looks like this:
SELECT * FROM questions_view WHERE `language` = "de" AND `#parent` IS NULL
UNION
SELECT * FROM questions_view WHERE `language` = "en" AND `#parent` IS NULL
AND id NOT IN (SELECT id
FROM questions_view
WHERE `language` = "de")
But I feel like this is not the optimal way of doing this. Any tips?
This:
or this:
Which if these queries is better depends on you database system and on how many questions in your database are translated.
See this series of articles in my blog for more detail: