我有以下数据和MySQL表:
CREATE TABLE my_tbl(
id INT,
dataset_query longtext
);
INSERT INTO my_tbl(id, dataset_query) VALUES (1, '{"database":1,"native":{"query":"SELECT * FROM view_1.device","template-tags":{}},"type":"native"}');
INSERT INTO my_tbl(id, dataset_query) VALUES (2, '{"database":1,"native":{"query":"SELECT id, name FROM view_1.request","template-tags":{}},"type":"native"}');
INSERT INTO my_tbl(id, dataset_query) VALUES (3, '{"database":3,"native":{"query":"SELECT id, name, age FROM view_3.person","template-tags":{}},"type":"native"}');
字符串
我需要更改dataset_query列中的以下数据:
- 从
"database":1
到"database":2
- 将
view_1
替换为view_2
为了更新数据库ID,我使用以下SQL语句:
UPDATE
my_tbl
SET
dataset_query = JSON_SET(dataset_query, "$.database", 2)
WHERE
json_extract(dataset_query, '$.database') = 1;
型
如何更新my_tbl
表中的dataset_query
列,以将所有出现的view_1
替换为view_2
?
预期结果如下:
| ID|数据集查询|
| --|--|
| 1 |{“database”:2,“native”:{“query”:“SELECT * FROM view_2.device”,“template-tagers”:{}},“type”:“native”:{“query”:“SELECT * FROM view_2.device”,“template-tagers”:{}}|
| 2 |{“database”:2,“native”:{“query”:“SELECT id,name FROM view_2.request”,“template-tagers”:{}},“type”:“native”}|
| 3 |{“database”:3,“native”:{“query”:“SELECT id,name,age FROM view_3.person”,“template-tagers”:{}},“type”:“native”}|
Db fiddle:https://www.db-fiddle.com/f/nw7sEBcF2i8eioWQawqdmD/0
2条答案
按热度按时间qvsjd97n1#
使用
REPLACE()
函数。使用路径$.native.query
获取嵌套对象属性。字符串
ryevplcw2#
字符串
Db fiddle:https://www.db-fiddle.com/f/t3jyZcYR33X9wHZPQ6sahf/1