次のことを行う PostgreSQL クエリを作成しようとしていますが、これまでの努力は無駄でした。
問題: A と B の 2 つのテーブルがあります。テーブル A からすべての列 (列: id、name、description) を選択し、"A.name" 列を列 "B.テーブル B の title" (列: id、table_A_id title、langcode)。ここで、B.table_A_id は 5 で、B.langcode は "nl" (行がある場合) です。
私の試み:
SELECT A.name,
case when exists(select title from B where table_A_id = 5 and langcode= 'nl')
then B.title
else A.name
END
FROM A, B
WHERE A.id = 5 and B.table_A_id = 5 and B.langcode = 'nl'
-- second try:
SELECT COALESCE(B.title, A.name) as name
from A, B
where A.id = 5 and B.table_A_id = 5 and exists(select title from B where table_A_id = 5 and langcode= 'nl')
CASE と COALESCE() を使用してみましたが、両方の概念に慣れていないために失敗しました。
前もって感謝します。