gpt4 book ai didi

sql - 如果另一列为真,Postgres 是否可以覆盖其中一列?

转载 作者:行者123 更新时间:2023-11-29 13:51:15 24 4
gpt4 key购买 nike

我有一个 Comment 表,我不想删除它,只是覆盖数据以破坏其中的信息。

我想添加一个 bool 值 is_deleted?,如果它等于 true,让它用“This comment”覆盖 :body 列已被删除”以便“删除”它但保留我需要的其他数据以保持关系。

有没有办法用 Postgres 做这样的事情?还是写一个 ruby​​ 方法来覆盖数据是唯一的解决方案?

最佳答案

我认为一个简单的方法是使用 View :

create view v_table as
select . . .,
(case when is_deleted then 'This comment has been deleted'
else comment
end) as comment
from t;

然后查询 View 而不是表以获得您想要的行为。

关于sql - 如果另一列为真,Postgres 是否可以覆盖其中一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com