The key of table items is comprised of (lang, id). Some items have a translated name in different languages. I want to create such a query that would return all the items from the DB in a given language and if an item doesn't have a translation to return the untranslated value.
Say I want to get all items written in Bulgarian. This is where I'm stuck:
SELECT lang, id, name
FROM items
WHERE lang = "bg" OR lang = "en"
GROUP BY id
The problem arises when there is an item, say, (1, "en") and an item (1, "bg"). The ids are the same. Then how does MySQL or SQLite determine which result to return? Is there any way I can tell it that I would rather prefer to return (1, "bg") if it exists but if it doesn't then (1, "en") would satisfy me?
P.S. To further illustrate what I want let's imagine that the database contains the following entries with schema (id, lang, name):
(1, "en", "abc")
(2, "en", "cde")
(3, "en", "def")
(1, "bg", "абв")
(3, "bg", "жзи")
After executing the desired query for Bulgarian I should get:
(1, "bg", "абв")
(2, "en", "cde")
(3, "bg", "жзи")