1
I’m building a multi-language base.
I have a select with the chosen language, but there is no guarantee that 100% of the content is translated, so I need to do a sort of or
to the default language as a secondary alternative.
select ...
where ...
and (idioma = 'pt-br' or idioma = 'en-us')
There is a relationship in this query, but that’s beside the point. The problem is that when I have entries in both languages, select
always returns the with smaller ID, in case en-us
:
id | idioma | texto
1 | en-us | ...
2 | pt-br | ...
3 | it | ...
What I need is to prioritize the desired language, and bring the default only when the desired one is not found.
If I understood what you want to do, I could just give you one
ORDER BY id
and aLIMIT 1
.– Oeslei
@Oeslei,
ORDER BY language = 'pt-br'
?– Papa Charlie
That. I had misread and understood that you wanted the smallest id. But that’s exactly what you put in. But order by doesn’t work that way. There’s a way to specify strings in order by, but I don’t remember now.
– Oeslei
I’ll test too, I feed soon.
– Papa Charlie
ORDER BY language = 'default language' is to solve. It can be != or DESC, if it is only one, or if there are several, you can sort by FIND_IN_SET. If you’re using a programming language to manage SQL, you can even leave the Ids string of a ready-to-use order in a Session (or variable) to avoid JOIN. (something like
ORDER BY FIND_IN_SET( id_linguagem_do_post, $string_com_ids_em_ordem de preferencia )
– Bacco
Thanks to the @Bacco tip, I’m limited with DB. There are quite a lot of references posted, and I’m seeing one by one - I’ll now see your about
FIND_IN_SET
. TKS– Papa Charlie
FIND_IN_SET will look something like this, if the desired language is it, then pt and last en-us if you don’t find the previous ones:
ORDER BY FIND_IN_SET( id_linguagem_da_postagem, '3,2,1') LIMIT 1
- can be 'it, en, en-us' too, if you want to use with JOIN, but if you use PHP or some other extra language, vc mounts the '3,2,1' once only as soon as the user logs in, and saves in the application for all queries instead of overloading the SGDB to any query.– Bacco