gpt4 book ai didi

sql - 检查是否存在于 SQL CASE 语句中

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:25 26 4
gpt4 key购买 nike

我正在尝试根据表中的不同列是否在表 b 的一组结果中来更新表 a 中的列。当前的变化:

update a
set a.field1 =
case
when exists (
select b.field2
from b
where b.field2 = a.field2
)
then 'FOO'
else 'BAR'
end

没有运行。对如何为 DB2 数据库执行此操作有任何想法吗?

编辑:感谢您的回答,我能做的就是

update a set field1 = 'FOO' where field2 in (select field2 from b);

update a set field1 = 'BAR' where field2 not in (select field2 from b);

但我会保持打开状态,以防有人可以在顶部找到可用的代码版本。

最佳答案

我在 DB2 for iSeries 机器上工作。试试这个:

update a
set a.field1 =
Coalesce( ( select 'FOO'
from b
where b.field2 = a.field2 ),
'BAR' )

Coalesce() 是一个返回列表中第一个非 NULL 的函数。

关于sql - 检查是否存在于 SQL CASE 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/565003/

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