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

时间:2018-04-09 12:57:54

标签: mysql sql json longtext

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

显然这没有用,我的问题是对于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}]     |
+------+-----------------------------------------------------------------------------------------------------------------------+ 

1 个答案:

答案 0 :(得分:0)

这样的事情也可行: -

select CASE WHEN 1=1 THEN ( select 1 FROM DUAL ) 
ELSE 0 END FROM DUAL;

REPLACE是一个MySQL字符串函数

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

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