首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL向MongoDB的妥协之JSON

MySQL向MongoDB的妥协之JSON

原创
作者头像
Jean
发布2018-10-17 16:38:41
3.3K1
发布2018-10-17 16:38:41
举报
文章被收录于专栏:Web行业观察Web行业观察

MySQL在5.7.8版本中增加了对json数据的支持,而不再是需要使用字符串形式进行存储。下面简单介绍下MySQL对json的操作:

1、数据类型--json

MySQL使用的字段数据类型就是json,例如(字段test_json):

create table `test_json`(
    id int not null auto_increment primary key, 
    test_json json not null
);

2、MySQL的json相关函数

① json_array()

作用:将数组转化为json格式数据。

语法:

/*为下一行打辅助*/
json_array(value1[, value2[, value3[,...]]])

例如:

insert into `test_json` values (
    null,
    json_array('Alan','Jane','Jack','Rose')
);

结果:

+----+----------------------------------+
| id | test_json                        |
+----+----------------------------------+
|  1 | ["Alan", "Jane", "Jack", "Rose"] |
+----+----------------------------------+

② json_object()

作用:将对象转化为json格式数据。

语法:

/*为下一行打辅助*/
json_object(key1, value1[, key2, value2[, key3, value3[, ...]]]);

例如:

insert into `test_json` values (
    null,
    json_object('name', 'Alan', 'age', '18', 'desc', 'handsome')
);

结果:

+----+-------------------------------------------------+
| id | test_json                                       |
+----+-------------------------------------------------+
|  2 | {"age": 18, "desc": "handsome", "name": "Alan"} |
+----+-------------------------------------------------+

③ json_quote()

作用:将字符串转化为json数据格式(为字符串增加双引号以及为引号增加转义字符) 语法:

/*为下一行打辅助*/
json_quote(value);

例如:

insert into `test_json` values(
    null,
    json_quote("Hello World!")
),(
    null,
    json_quote('He say, "Hello World!"')
);

结果:

+----+----------------------------+
| id | test_json                  |
+----+----------------------------+
|  6 | "Hello World!"             |
|  7 | "He Say, \"Hello World!\"" |
+----+----------------------------+

④ json_unquote()

作用:与json_quote()作用相反 语法:

/*为下一行打辅助*/
json_unquote(value);

例如:

select id, json_unquote(test_json) as test_json 
from `test_json` 
where id=6 or id=7;

结果:

+----+------------------------+
| id | test_json              |
+----+------------------------+
|  6 | Hello World!           |
|  7 | He Say, "Hello World!" |
+----+------------------------+

⑤ json_merge()

作用:将多个json文本合并成一个json文本 语法:

/*助攻行*/
json_merge(json1, json2[,json3[, json4[, ...]]]);

例如:

insert into `test_json` values(
    null,
    json_merge('["Alan","Jane"]', '["Jack", "Rose"]', '["other"]')
);

结果:

+----+-------------------------------------------+
| id | test_json                                 |
+----+-------------------------------------------+
| 10 | ["Alan", "Jane", "Jack", "Rose", "other"] |
+----+-------------------------------------------+

⑥ json_valid()

作用:判断json格式是否正确 语法:

/*助攻行*/
json_valid(json);

例如:

/*助攻行*/
select *,json_valid(test_json) as IsValid from test_json;

结果:

+----+-------------------------------------------------+---------+
| id | test_json                                       | IsValid |
+----+-------------------------------------------------+---------+
|  1 | ["Alan", "Jane", "Jack", "Rose"]                |       1 |
|  2 | {"age": 18, "desc": "handsome", "name": "Alan"} |       1 |
|  4 | "Hello World!"                                  |       1 |
|  5 | "He Say, 'Hello World!'"                        |       1 |
|  6 | "Hello World!"                                  |       1 |
|  7 | "He Say, \"Hello World!\""                      |       1 |
|  8 | ["Alan", "Jane", "Jack", "Rose", "other"]       |       1 |
|  9 | ["Alan", "Jane", "Jack", "Rose", "other"]       |       1 |
| 10 | ["Alan", "Jane", "Jack", "Rose", "other"]       |       1 |
+----+-------------------------------------------------+---------+

⑦ json_type()

作用:判断json文本的数据类型 语法:

/*助攻行*/
json_type(jsonData);

例如:

/*助攻行*/
select *,json_type(test_json) as json_type from test_json;

结果:

+----+-------------------------------------------------+-----------+
| id | test_json                                       | json_type |
+----+-------------------------------------------------+-----------+
|  1 | ["Alan", "Jane", "Jack", "Rose"]                | ARRAY     |
|  2 | {"age": 18, "desc": "handsome", "name": "Alan"} | OBJECT    |
|  4 | "Hello World!"                                  | STRING    |
|  5 | "He Say, 'Hello World!'"                        | STRING    |
|  6 | "Hello World!"                                  | STRING    |
|  7 | "He Say, \"Hello World!\""                      | STRING    |
|  8 | ["Alan", "Jane", "Jack", "Rose", "other"]       | ARRAY     |
|  9 | ["Alan", "Jane", "Jack", "Rose", "other"]       | ARRAY     |
| 10 | ["Alan", "Jane", "Jack", "Rose", "other"]       | ARRAY     |
+----+-------------------------------------------------+-----------+

暂时只写这7个函数,MySQL还有许多关于json的函数,例如:json_append(), json_array_append(), json_array_insert(), json_insert(), json_remove(), json_replace(), json_set() 等

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、数据类型--json
  • 2、MySQL的json相关函数
    • ① json_array()
      • ② json_object()
        • ③ json_quote()
          • ④ json_unquote()
            • ⑤ json_merge()
              • ⑥ json_valid()
                • ⑦ json_type()
                相关产品与服务
                云数据库 SQL Server
                腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档