sql 更新/替换行,使某些列不受影响
我想执行行更新/替换,以便未指定值的列保持原样,例如对于 4 列表:
-- expecting row = 99|a|b|?
replace into game_data ( id, col_a, col_b ) values ( 99, "a", "b" );
-- expecting row = 99|a|b2|c < a remains unchanged
replace into game_data ( id, col_b, col_c ) values ( 99, "b2", "c" );
这可能吗?如果没有,我猜测替代方法是提取现有行,根据需要合并数据,然后执行替换,但这似乎是数据库应该做的事情?一个模式的东西?
(对于 bash 脚本中使用的 sqlite3)
I would like to perform an row update / replace so that columns for which values are not specified remain as they were e.g. for a 4 column table:
-- expecting row = 99|a|b|?
replace into game_data ( id, col_a, col_b ) values ( 99, "a", "b" );
-- expecting row = 99|a|b2|c < a remains unchanged
replace into game_data ( id, col_b, col_c ) values ( 99, "b2", "c" );
Is this possible? If not, I am guessing the alternative would be to extract the existing row, merge the data as required then perform the replace, but this seems like something a db should do?? a schema thing?
(for sqlite3 being used from bash script)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 UPDATE 语句,如下所示:
这将为 id 为 99 的行设置 col_a 和 col_b 的值,而所有其他列值保持不变。
(注意:REPLACE() 是一个字符串替换函数)。注意:不幸的是,REPLACE 在不同的 SQL 方言中意味着不同的东西。
Use the UPDATE statement, like this:
This will set values for col_a and col_b for the row with id of 99, leaving all other column values unchanged.
(NOTE: REPLACE() is a string replacement function).NOTE: REPLACE means different things in different SQL dialects, unfortunately.