我有两张表,需要加入。他们看起来像这样
我需要加入它们,以便我拥有两个表中的所有数据,然后显示这样的信息 - 如果有德语翻译,则仅显示德语。如果没有德语,则显示英语(适用于所有对象)
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'
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
如果不仅仅是英语和德语,最后一行应更改为
WHERE f2.id IS NULL AND (f1.lang='ger' OR f1.lang='eng')
尝试:
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