sql - Select rows WHERE any row in column is equal to 1 or if not, column is equal to 2, but not both -
i have got translation table text like:
table: todos
day | text_id ------------- 0 | 1 1 | 2 1 | 1
table: translations
lang | text_id | text --------------------- deu | 1 | laufen eng | 1 | running eng | 2 | swimming
now want lookup todos in german (deu). problem is, don´t have translation (e.g.) text_id 2: swimming in german.
my default query be:
select todos.day, translations.text inner join translations on todos.text_id = translations.text_id translations.locale = 'deu';
i get:
day | text -------------- 0 | laufen 1 | laufen
but want:
day | text -------------- 0 | laufen 1 | swimming 1 | laufen
how can missing rows? first should needed rows with:
select todos.day, translations.text inner join translations on todos.text_id = translations.text_id translations.locale = 'deu' or translations.locale = 'eng';
and remove 'eng' duplications - how?
sorry terrible title, don´t know how describe ...
you need left join
keep records in first table. need twice english records default:
select td.day, coalesce(tdeu.text, teng.text) text todos td left join translations tdeu on td.text_id = tdeu.text_id , tdeu.locale = 'deu' left join translations teng on td.text_id = teng.text_id , teng.locale = 'eng';
Comments
Post a Comment