Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite - conditional SELECT?

I have a table with languages.

I would like to make a query that gets rows with specified language, but if it won't return any data - with default language.

Is this possible to do in one query?

UPDATE:

My table: Language Fields:

id - (primary key, unique),

lang - (text, not unique),

content - (text, not unique).

and my current query is:

SELECT * FROM Languages WHERE (id='1') AND (lang = 'es') 

As I mentioned before if above query won't return anything I would like to get results form default language:

SELECT * FROM Languages WHERE (id='1') AND (lang = 'en') 

'en' is default language in this example.

like image 625
pixel Avatar asked Jan 18 '23 06:01

pixel


1 Answers

Something like this:

(SELECT CASE
    WHEN ((SELECT ID FROM table WHERE language = specified language) IS NULL)
    THEN
      default language
    ELSE
      specified language
    END);

It will return your desired language, based on the result of the subquery. Plug that into the WHERE clause of your final query.

like image 149
Robert Harvey Avatar answered Jan 25 '23 05:01

Robert Harvey