user1807772
user1807772

Reputation:

How to make kind of IF in SQL query

I have two tables, that need to be joined. They look like this

enter image description here

enter image description here

I need to join them so I have all the data from both tables and then display the information like this - IF there is German translation, display ONLY German. If there is no German, display English (Which is available for all the objects)

Upvotes: 1

Views: 97

Answers (3)

Jakub Kania
Jakub Kania

Reputation: 16487

SELECT f1.id,f1.lang,f1.name
FROM foo AS f1
LEFT JOIN foo AS f2
ON f1.id=f2.id AND f1.lang<>f2.lang AND f2.lang='ger'
WHERE f2.id IS NULL

http://sqlfiddle.com/#!2/a0a75/9

If there are more than just english and german language the last line should be changed to

WHERE f2.id IS NULL AND (f1.lang='ger' OR f1.lang='eng')

Upvotes: 1

user359040
user359040

Reputation:

Try:

select entry_id,
       case max(lang)
           when 'ger' then max(case lang when 'ger' then name end)
           else max(name)
       end as trans_name
from your_table
group by entry_id

Upvotes: 1

Andomar
Andomar

Reputation: 238078

select  eng.entryid
,       coalesce(ger.name, eng.name)
from    YourTable eng
left join
        YourTable ger
on      ger.entryid = eng.entryid
        and ger.lang = 'ger'
where   eng.lang = 'eng'

Upvotes: 3

Related Questions