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

Update non-null field with possibly null values

在下面的查询中:

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) 可能 return NULL 而字段 hidNOT NULL.

如何修改语句,以便在子查询 return 为 NULL 时 跳过 而不是使整个执行失败?

使用UPDATE IGNORE解决了我的问题。但是它会产生警告信息。

您可以使用 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