如何加入两个 table 并使用第二个 table 覆盖第一个?

How to join two table and using the second table to override the first one?

我正在开发一个待办事项应用程序,其中有多个项目共享一个预配置的清单,我想要的是能够修改每个项目包含的清单。

我的想法是有 2 个单独的 tables:1 个用于共享,1 个用于每个项目的修改

CREATE TABLE checklist (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
    title text NOT NULL,
    description text NOT NULL,
);

CREATE TABLE modification (
    project_uuid uuid,
    id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
    title text NOT NULL,
    description text NOT NULL,
);

每个项目都有自己的清单,基于用户可以修改的预配置清单,对共享清单所做的任何修改都会反映到所有项目。

有没有办法使用第二个“修改”table在“检查表”table之上进行修改?如果在“修改”中没有找到某一行,它将从“清单”中获取它,并且如果在两个 table 中都存在一行,则“修改”中的行将覆盖“清单”中的行?

例如,如果我有:

SELECT * FROM checklist;
id | title  | description
---+--------+-------------
 1 | Item 1 | Dummy item
 2 | Item 2 | Dummy item

SELECT * FROM modification;
project_uuid | id | title  | description          |
-------------+----+--------+----------------------+
  1          | 2  | Item 2 | Modified description |

我想要这个结果

 id |  title | description          |
----+--------+----------------------+
  1 | Item 1 | Dummy item           |
  2 | Item 2 | Modified description |

提前致谢

使用左连接和coalesce()函数:

select  c.id, 
        coalesce(m.title, c.title)             as title, 
        coalesce(m.description, c.description) as description
  from checklist c 
       left join modification m on c.id=m.id