Mis*_*s T 14 sql null sql-update
我一直在使用这样的SQL来更新我的数据库中的属性列表:
update my_table set a = ?, b = ?, c = ?, d = ?, where customer = ?
Run Code Online (Sandbox Code Playgroud)
但是我想要仅在数据库中没有值的情况下更新具有新值的属性.我怎样才能做到这一点?
Grz*_*lik 26
在MS SQL中这样的事情(假设非值意味着数据库NULL
)应该工作:
update
my_table
set
a = COALESCE(a, ?),
b = COALESCE(b, ?),
c = COALESCE(c, ?),
d = COALESCE(d, ?)
where
customer = ?
Run Code Online (Sandbox Code Playgroud)
COALESCE()
从其参数返回第一个非null值.
在 MySQL 中,你可以这样做:
UPDATE my_table
SET
a = IFNULL(a, ?),
b = IFNULL(b, ?),
c = IFNULL(c, ?),
d = IFNULL(d, ?)
where customer = ?
Run Code Online (Sandbox Code Playgroud)
如果您使用的是甲骨文:
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 = ?
Run Code Online (Sandbox Code Playgroud)
nvl
如果您没有使用 Oracle,请使用您正在使用的 RDBMS 更新问题,或者在数据库中查找类似的函数。
归档时间: |
|
查看次数: |
15952 次 |
最近记录: |