SQL UPDATE,但仅当旧值为 null 时

发布于 2024-10-14 12:58:12 字数 172 浏览 3 评论 0原文

我一直在使用这样的 sql 来更新数据库中的属性列表:

update my_table set a = ?, b = ?, c = ?, d = ?,  where customer = ?

但我只想在数据库中没有值的情况下用新值更新属性。我怎样才能做到这一点?

I have been using a sql like this to update a list of properties in my database:

update my_table set a = ?, b = ?, c = ?, d = ?,  where customer = ?

But I want to update a property with a new value ONLY if it does not have a value in the database already. How can I do that?

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

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

发布评论

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

评论(7

弄潮 2024-10-21 12:58:12

在 MS SQL 中,类似这样的内容(假设非值意味着数据库 NULL)应该起作用:

update 
  my_table 
set 
  a = COALESCE(a, ?), 
  b = COALESCE(b, ?), 
  c = COALESCE(c, ?), 
  d = COALESCE(d, ?)
where 
  customer = ?

COALESCE() 从其参数返回第一个非空值。

In MS SQL something like this (assuming non value means database NULL) should work:

update 
  my_table 
set 
  a = COALESCE(a, ?), 
  b = COALESCE(b, ?), 
  c = COALESCE(c, ?), 
  d = COALESCE(d, ?)
where 
  customer = ?

COALESCE() returns first non null value from its arguments.

掩饰不了的爱 2024-10-21 12:58:12

在 MySQL 中,你可以这样做:

UPDATE my_table 
SET
a = IFNULL(a, ?),
b = IFNULL(b, ?),
c = IFNULL(c, ?),
d = IFNULL(d, ?)
where customer = ?

In MySQL, you could do:

UPDATE my_table 
SET
a = IFNULL(a, ?),
b = IFNULL(b, ?),
c = IFNULL(c, ?),
d = IFNULL(d, ?)
where customer = ?
哭了丶谁疼 2024-10-21 12:58:12

如果您正在谈论在行中的每个字段的基础上执行此操作:

update my_table
set a = coalesce(a, ?),
    b = coalesce(b, ?),
    c = coalesce(c, ?)
where customer = ?

If you're talking about doing that on a field per field basis in the row:

update my_table
set a = coalesce(a, ?),
    b = coalesce(b, ?),
    c = coalesce(c, ?)
where customer = ?
偏爱自由 2024-10-21 12:58:12

如果您使用的是 Oracle:

update my_table 
   set a = nvl(a, new_a_value),
       b = nvl(b, new_b_value),
       c = nvl(c, new_c_value),
       d = nvl(d, new_d_value),
 where customer = ?

如果您没有使用 Oracle,请使用您正在使用的 RDBMS 更新问题,或者在数据库中查找类似 nvl 的函数。

If you are using oracle:

update my_table 
   set a = nvl(a, new_a_value),
       b = nvl(b, new_b_value),
       c = nvl(c, new_c_value),
       d = nvl(d, new_d_value),
 where customer = ?

If you are not using Oracle, please update question with RDBMS you are using, or look for a nvl like function in your database.

小梨窩很甜 2024-10-21 12:58:12

在查询中使用“is null”或“is not null”的组合,即

update my_table set a = ?其中客户 = ? a 为 null

当然,这仅适用于 null 为合法值的列。如果不知道各个列的约束,实际上很难确切地知道什么查询适合您。

Use combinations of "is null" or "is not null" in your queries, i.e.

update my_table set a = ? where customer = ? and a is null

Of course, this only works for columns where null is a legal value. It's actually hard to know exactly what query will work for you without knowing the constraints on the various columns.

陌伤浅笑 2024-10-21 12:58:12
update YourTable
    set a = coalesce(a, NewValueA),
        b = coalesce(b, NewValueB),
        ...
    where customer = ?
update YourTable
    set a = coalesce(a, NewValueA),
        b = coalesce(b, NewValueB),
        ...
    where customer = ?
失而复得 2024-10-21 12:58:12

尝试这个代码SQL本机它对我来说非常有用:

UPDATE table 
SET field = 'NO'
WHERE field 
IS NULL
OR field = ''

仅更新NULL值或空。

Try this code SQL native it's work for me very well :

UPDATE table 
SET field = 'NO'
WHERE field 
IS NULL
OR field = ''

Update just NULL value or empty.

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