用可能为空值更新非空字段

发布于 2025-01-14 15:34:51 字数 560 浏览 4 评论 0 原文

在下面的查询中:

update collect_irc_deploy c
set hid = (select id
           from auth_hierarchy
           where fqdn = (select location
                         from reserve
                         where id=c.rid
                        )
          )
where hid = 0 and rid is not null

子查询 select id from auth_hierarchy where fqdn = (select location from Reserve where id = c.rid) 可能返回 NULL 而字段 hid NOT NULL

如何修改语句,以便在子查询返回 NULL 时跳过该数据项,而不是使整个执行失败?

In the query below:

update collect_irc_deploy c
set hid = (select id
           from auth_hierarchy
           where fqdn = (select location
                         from reserve
                         where id=c.rid
                        )
          )
where hid = 0 and rid is not null

the subquery select id from auth_hierarchy where fqdn = (select location from reserve where id = c.rid) may return NULL while the field hid is NOT NULL.

How can I modify the statement so that if the subquery returns NULL that data item is skipped instead of failing the entire execution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2025-01-21 15:34:51

您可以使用 update...join 语法来确保仅更新连接的行:

update collect_irc_deploy
join reserve on reserve.id = collect_irc_deploy.rid
join auth_hierarchy on auth_hierarchy.fqdn = reserve.location
set collect_irc_deploy.hid = auth_hierarchy.id
where collect_irc_deploy.hid = 0 and collect_irc_deploy.rid is not null

You can use update...join syntax to ensure only joined rows are updated:

update collect_irc_deploy
join reserve on reserve.id = collect_irc_deploy.rid
join auth_hierarchy on auth_hierarchy.fqdn = reserve.location
set collect_irc_deploy.hid = auth_hierarchy.id
where collect_irc_deploy.hid = 0 and collect_irc_deploy.rid is not null
開玄 2025-01-21 15:34:51

使用 UPDATE IGNORE 解决了我的问题。但它会生成警告消息。

Use UPDATE IGNORE solved my problem. But it will generate warning messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文