gpt4 book ai didi

sql - 在 Postgres 中使用条件 where 子句更新多行?

转载 作者:行者123 更新时间:2023-11-29 14:25:49 24 4
gpt4 key购买 nike

我试图在单个查询中更新多行,因为我有很多行要同时更新。在我的查询中,有一个仅适用于特定行的 where 子句。

例如,我有以下查询:

update mytable as m set
column_a = c.column_a,
column_b = c.column_b
from (values
(1, 12, 6, TRUE),
(2, 1, 45, FALSE),
(3, 56, 3, TRUE)
) as c(id, column_a, column_b, additional_condition)
where c.id = m.id
and CASE c.additional_condition when TRUE m.status != ALL(array['active', 'inactive']) end;

where 子句中的最后一行 (m.status != ALL(array['active', 'inactive'])) 应该只应用于行c.additional_condition 的值为 TRUE。否则,不应应用该条件。

是否可以在 Postgres 中实现这一点?

最佳答案

我想这就是你想要的:

and CASE  
when c.additional_condition THEN m.status != ALL(array['active', 'inactive'])
else TRUE
end

关于sql - 在 Postgres 中使用条件 where 子句更新多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157492/

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