I'm not used to query sentences and this one is a bit tricky, I don't know even if it is possible (at least with just a single sql query) or if I should change the question name to something more accurated, if so let me know.
I have two tables, I will make them simple. The first one that is about games has a name and a details_id, the other on that consists on the game details has details_id (foreign key), locale and description.
| Games |
|---|
| name |
| details_id |
| Details |
|---|
| details_id |
| locale |
| description |
A game can have multiple details associated as the details_id is not unique. So I can have 3 Details tables with same id associated to a Game but there is no Game with the same details_id as other. I will make an example:
GAMES
| name | details_id |
|---|---|
| Mario Kart | 007 |
DETAILS
| details_id | locale | description |
|---|---|---|
| 007 | en-GB | A nice racing game. |
| details_id | locale | Description |
|---|---|---|
| 007 | es-ES | Un buen juego de carreras. |
I show all the Games and their english descriptions in a view, they are now sorted by description as follows:
SELECT Games.name, Details.description
FROM Games
INNER JOIN Details ON Details.locale='en-GB'
The trick is that I want it to depend on the locale, they might be games not translated to the same locale as others.
The goal is a select that through a join, can select all the Game names and Detail descriptions ordered by description where locale is es-ES, and if the detail does not exist, it will take the en-GB one still ordering alphabetically among spanish descriptions.