用于更新列并替换值的 SQLite 查询 [英] SQlite query to update column and replace a value

查看:19
本文介绍了用于更新列并替换值的 SQLite 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新一个包含像 12,43,433 这样的字符串的列 我只想用另一个数字(比如 54)替换 43,这样列值就变成了 12,54,433.

I want to update a column that contains a string like 12,43,433 I want to only replace 43 with another number say 54 so that the column value becomes 12,54,433.

我该怎么做??

推荐答案

将列表存储为字符串是一个非常的坏主意.SQL 有一个很好的数据结构来存储列表.它被称为表,而不是字符串.存储列表的正确方法是使用连接表.

Storing lists as strings is a very bad idea. SQL has a great data structure for storing lists. It is called a table, not a string. The proper way to store lists is to use a junction table.

有时我们会被其他人的非常糟糕的设计决定所困.如果是这样,你可以这样做:

Sometimes we are stuck with other people's really bad design decisions. If so, you can do:

update t
    set col = trim(replace(',' || col || ',', ',43,', ',54,'), ',')
    where ',' || col || ',' like '%,43,%';

注意事项:

  • 无论43"出现在字符串的哪个位置(包括开头和结尾),这都有效.
  • 这会保持字符串的格式,开头和结尾都没有逗号.
  • 这只会尝试更新列表中包含特定元素的行.

在您弄清楚如何修复数据结构时,使用这样的查询确实应该是权宜之计.

Use of such a query should really be a stopgap while you figure out how to fix the data structure.

这篇关于用于更新列并替换值的 SQLite 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆