如何附加到现有记录?

How can I append to an existing record?

我在名为 notes 的架构中有一列。它包含应该在每次更新记录时更新的修订说明。如何在不覆盖的情况下将数据附加到现有条目?

例如,目前显示:

"09022014, Updated user's GSDA ID."

在我运行我的查询之后,我想看到:

"09022014, Updated user's GSDA ID. 04062015, Updated TTFIA building field."

将新注释与当前注释连接 (||):

update tablename set notes = notes || " new note"
where id = 123;

哦,别忘了正则表达式:

update tablename set notes = 
regexp_replace('notes', '^(.*)$', '' || ' note 2')
where id = 123;

这揭示了使用 regexp_replace 的意外结果,如果您有兴趣,我会在此处询问:

使用串联运算符将现有列值与新值串联 ||.

"09022014, Updated user's GSDA ID. 04062015, Updated TTFIA building field."

如果您有双引号和值本身,那么您还需要注意一件事,即 TRIM 现有列值右侧的双引号

当然,您需要在现有值和新值之间保持 space。

例如,

UPDATE table_name 
   SET notes = rtrim(notes, '"') || ' ' || new_note || '"';