SQL UPDATE,但仅当旧值为 null 时
我一直在使用这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 MS SQL 中,类似这样的内容(假设非值意味着数据库
NULL
)应该起作用:COALESCE()
从其参数返回第一个非空值。In MS SQL something like this (assuming non value means database
NULL
) should work:COALESCE()
returns first non null value from its arguments.在 MySQL 中,你可以这样做:
In MySQL, you could do:
如果您正在谈论在行中的每个字段的基础上执行此操作:
If you're talking about doing that on a field per field basis in the row:
如果您使用的是 Oracle:
如果您没有使用 Oracle,请使用您正在使用的 RDBMS 更新问题,或者在数据库中查找类似
nvl
的函数。If you are using oracle:
If you are not using Oracle, please update question with RDBMS you are using, or look for a
nvl
like function in your database.在查询中使用“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.
尝试这个代码SQL本机它对我来说非常有用:
仅更新NULL值或空。
Try this code SQL native it's work for me very well :
Update just NULL value or empty.