更新 MySQL 长文本数据类型中的一个字段
Update one field in MySQL longtext data type
JSON 数据库中存储了一些 JSON 数据,table 架构如下:-
create TABLE ApplicationEntity (
-> name varchar(100),
-> jsonData longtext
-> ) ENGINE=InnoDB Default CHARSET=latin1;
table 中 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}] |
+------+-------------------------------------------------------------------------------------------------------------------+
显然这没有用,我的问题是对于长文本数据类型,我们如何才能对单个字段进行更新。
所以 table 应该采用这种形式:
+------+-----------------------------------------------------------------------------------------------------------------------+
| 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}] |
+------+-----------------------------------------------------------------------------------------------------------------------+
像这样的东西也可以:-
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 函数不支持正则表达式。
JSON 数据库中存储了一些 JSON 数据,table 架构如下:-
create TABLE ApplicationEntity (
-> name varchar(100),
-> jsonData longtext
-> ) ENGINE=InnoDB Default CHARSET=latin1;
table 中 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}] |
+------+-------------------------------------------------------------------------------------------------------------------+
显然这没有用,我的问题是对于长文本数据类型,我们如何才能对单个字段进行更新。
所以 table 应该采用这种形式:
+------+-----------------------------------------------------------------------------------------------------------------------+
| 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}] |
+------+-----------------------------------------------------------------------------------------------------------------------+
像这样的东西也可以:-
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 函数不支持正则表达式。