更新mysql longtext数据类型中的一个字段

db2dz4w8  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(748)

mysql数据库中存储了一些json数据,表模式如下this:-

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

但是table变成了这样

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}] |
+------+-------------------------------------------------------------------------------------------------------------------+

显然这不起作用,我的问题是对于longtext数据类型,我们如何才能对单个字段进行更新。
所以表格应该是这样的:

+------+-----------------------------------------------------------------------------------------------------------------------+
| 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}]     |
+------+-----------------------------------------------------------------------------------------------------------------------+
eit6fx6z

eit6fx6z1#

像这样的事情work:-

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函数不支持正则表达式。

相关问题