0

MySQL数据库中存储了一些JSON数据,表模式如下:-

create TABLE ApplicationEntity (
    ->  name varchar(100),
    ->  jsonData longtext
    ->  ) ENGINE=InnoDB Default CHARSET=latin1;

表中 jsonData 的一个示例条目是这样的

[{"name":"some_name","description":"good_description","gla":"None","srcPort":"123","dstPort":"2345","disableTimeout":false"}]

我们如何访问更新的单个键和值对。

我尝试过这样的事情:-

mysql> Update ApplicationEntity set jsonData='%"gla":"google"%' where jsonData like '%"gla":"None"%';
Query OK, 5 rows affected (0.02 sec)
Rows matched: 5  Changed: 5  Warnings: 0

但是表格变成了这种形式

mysql> select * from ApplicationEntity;
+------+-------------------------------------------------------------------------------------------------------------------+
| name | jsonData                                                                                                     |
+------+-------------------------------------------------------------------------------------------------------------------+
| a1   | %"gla":"google"%                                                                                                    |
| a2   | %"gla":"google"%                                                                                                    |
| a2   | %"gla":"google"%                                                                                                    |
| a2   | %"gla":"google"%                                                                                                    |
| a2   | %"alg":"google"%                                                                                                    |
| a2   | [{"name":"CREATED_IN_CHROME","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2   | [{"name":"CREATED_IN_MOZILL","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
+------+-------------------------------------------------------------------------------------------------------------------+

显然这没有用,我的问题是对于长文本数据类型,我们如何才能对各个字段进行更新。

所以表格应该变成这种形式:

+------+-----------------------------------------------------------------------------------------------------------------------+
| name | jsonData                                                                                                         |
+------+-----------------------------------------------------------------------------------------------------------------------+
| a1   | [{"name":"CREATED_IN_IE","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}]         |
| a2   | [{"name":"CREATED_IN_SAFARI","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}]     |
| a2   | [{"name":"CREATED_IN_OMERTA","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}]     |
| a2   | [{"name":"CREATED_IN_duckduckgo","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2   | [{"name":"CREATED_IN_ucbrowser","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}]  |
| a2   | [{"name":"CREATED_IN_CHROME","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}]     |
| a2   | [{"name":"CREATED_IN_MOZILL","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}]     |
+------+-----------------------------------------------------------------------------------------------------------------------+ 
4

1 回答 1

0

像这样的东西也可以工作: -

UPDATE ApplicationEntity
SET jsonData = REPLACE(jsondata, 'None', 'google')
WHERE jsonData LIKE '%"gla":"None"%'

其中 REPLACE 是 MySQL 字符串函数

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

另外我们应该知道 MySQL REPLACE 函数不支持正则表达式。

于 2018-04-09T17:22:37.103 回答