富⽂本保存到mysql中⽤什么字段类型_富⽂本编辑器内容在mysql数据库以什么类型保..。。。
卢舍那的光明
2019-11-08 11:10
4135
JSON (JavaScriptObject Notation) 是⼀种轻量级的数据交换格式,主要⽤于传送数据。JSON采⽤了独⽴于语⾔的⽂本格式,类似XML,但是⽐XML简单,易读并且易编写。对机器来说易于解析和⽣成,并且会减少⽹络带宽的传输。由于JSON格式可以解耦javascript 客户端应⽤与Restful服务器端的⽅法调⽤,因⽽在互联⽹应⽤中被⼤量使⽤。
JSON的格式⾮常简单:名称/键值。之前MySQL版本⾥⾯要实现这样的存储,要么⽤VARCHAR要么⽤TEXT⼤⽂本。 MySQL5.7发布后,专门设计了JSON数据类型以及关于这种类型的检索以及其他函数解析。我们先看看MySQL⽼版本的JSON存取。
⽰例表结构:
CREATE TABLE json_test(
id INT,
person_desc TEXT
)ENGINE INNODB;json值的类型有哪些
我们来插⼊⼀条记录:
INSERT INTO json_test VALUES (1,''{
"programmers": [{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
}, {
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
}, {
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
}],
"authors": [{
"firstName": "Isaac",
"lastName": "Asimov",
"genre": "sciencefiction"
}, {
"firstName": "Tad",
"lastName": "Williams",
"genre":"fantasy"
}, {
"firstName": "Frank",
"lastName": "Peretti",
"genre": "christianfiction"
}],
"musicians": [{
"firstName": "Eric",
"lastName": "Clapton",
"instrument": "guitar"
}, {
"firstName": "Sergei",
"lastName": "Rachmaninoff",
"instrument": "piano"
}]
}'');
那⼀般我们遇到这样来存储JSON格式的话,只能把这条记录取出来交个应⽤程序,由应⽤程序来解析。如此⼀来,JSON⼜和特定的应⽤程序耦合在⼀起,其便利性的优势⼤打折扣。
现在到了MySQL5.7,可以⽀持对JSON进⾏属性的解析,我们重新修改下表结构:
ALTER TABLE json_test MODIFY person_desc json;
先看看插⼊的这⾏JSON数据有哪些KEY:
mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G
*************************** 1. row***************************
id: 1
keys: ["authors", "musicians","programmers"]
1 row in set (0.00 sec)
我们可以看到,⾥⾯有三个KEY,分别为authors,musicians,programmers。那现在⼀个KEY把对应的值拿出来:
mysql> SELECT json_extract(AUTHORS,''$.lastName[0]'') AS ''name'', AUTHORS FROM
-> (
-> SELECT id,json_extract(person_desc,''$.authors[0][0]'') AS "authors" FROM json_test ->UNION ALL
-> SELECT id,json_extract(person_desc,''$.authors[1][0]'') AS "authors" FROM json_test -> UNION ALL
-> SELECT id,json_extract(person_desc,''$.authors[2][0]'') AS "authors" FROM json_test -> ) AS T1
-> ORDER BY NAME DESC\G
*************************** 1. row***************************
name:"Williams"
AUTHORS: {"genre": "fantasy","lastName": "Williams", "firstName":"Tad"}
*************************** 2. row***************************
name:"Peretti"
AUTHORS: {"genre":"christianfiction", "lastName": "Peretti","firstName": "Frank"}
*************************** 3. row***************************
name:"Asimov"
AUTHORS: {"genre": "sciencefiction","lastName": "Asimov", "firstName":"Isaac"}
3 rows in set (0.00 sec)
现在来把详细的值罗列出来:
mysql> SELECT
->json_extract(AUTHORS,''$.firstName[0]'') AS "firstname",
-> json_extract(AUTHORS,''$.lastName[0]'')AS "lastname",
-> json_extract(AUTHORS,''$.genre[0]'') AS"genre"
-> FROM
-> (
-
> SELECT id,json_extract(person_desc,''$.authors[0]'')AS "authors" FROM json
_test
-> ) AS T\G
*************************** 1. row***************************
firstname: "Isaac"
lastname:"Asimov"
genre:"sciencefiction"
1 row in set (0.00 sec)
我们进⼀步来演⽰把authors 这个KEY对应的所有对象删掉。
mysql> UPDATE json_test
-> SET person_desc =json_remove(person_desc,''$.authors'')\G
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查下对应的KEY,发现已经被删除掉了。
mysql> SELECT json_contains_path(person_desc,''all'',''$.authors'')as authors_exists FROM json_test\G
*************************** 1. row***************************
authors_exists: 0
1 row in set (0.00 sec)
总结下,虽然MySQL5.7开始⽀持JSON数据类型,但是我建议如果要使⽤的话,最好是把这样的值取出来,然后在应⽤程序段来计算。毕竟数据库是⽤来处理结构化数据的,⼤量的未预先定义schema的json解析,会拖累数据库的性能。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。