- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章MySQL中json字段的操作方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
MySQL5.7.8中引入了json字段,这种类型的字段使用的频率比较低,但是在实际操作中,有些业务仍然在用,我们以此为例,介绍下json字段的操作方法:
还是从例子看起:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
mysql>
create
table
test1(id
int
,info json);
Query OK, 0
rows
affected (0.02 sec)
mysql>
insert
into
test1
values
(1,
'{"name":"yeyz","age":26}'
),(2,
'{"name":"zhangsan","age":30}'
),(3,
'{"name":"lisi","age":35}'
);
Query OK, 3
rows
affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql>
select
*
from
test1;
+
------+---------------------------------+
| id | info |
+
------+---------------------------------+
| 1 | {
"age"
: 26,
"name"
:
"yeyz"
} |
| 2 | {
"age"
: 30,
"name"
:
"zhangsan"
} |
| 3 | {
"age"
: 35,
"name"
:
"lisi"
} |
+
------+---------------------------------+
3
rows
in
set
(0.00 sec)
|
首先我们创建了一个表test1,其中id是int字段,info是json字段,插入了三条数据,如上:
1
2
3
4
5
6
7
8
|
mysql>
select
*
from
test1
where
json_extract(info,
"$.age"
)>=30;
+
------+---------------------------------+
| id | info |
+
------+---------------------------------+
| 2 | {
"age"
: 30,
"name"
:
"zhangsan"
} |
| 3 | {
"age"
: 35,
"name"
:
"lisi"
} |
+
------+---------------------------------+
2
rows
in
set
(0.00 sec)
|
我们可以通过json_extract的方法得到json中的内容。其中:
1、$符号代表的是json的根目录, 。
2、我们使用$.age相当于取出来了json中的age字段, 。
3、当然,在函数最前面,应该写上字段名字info 。
下面来看json中常用的函数:
a、json_valid判断是否是json字段,如果是,返回1,如果不是,返回0 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
mysql>
select
json_valid(2);
+
---------------+
| json_valid(2) |
+
---------------+
| 0 |
+
---------------+
1 row
in
set
(0.01 sec)
mysql>
select
json_valid(
'{"num":2}'
);
+
-------------------------+
| json_valid(
'{"num":2}'
) |
+
-------------------------+
| 1 |
+
-------------------------+
1 row
in
set
(0.00 sec)
mysql>
select
json_valid(
'2'
);
+
-----------------+
| json_valid(
'2'
) |
+
-----------------+
| 1 |
+
-----------------+
1 row
in
set
(0.00 sec)
mysql>
select
json_valid(
'name'
);
+
--------------------+
| json_valid(
'name'
) |
+
--------------------+
| 0 |
+
--------------------+
1 row
in
set
(0.00 sec)
|
这里需要注意的是,如果传入了字符串2,那么,返回结果是1 。
b、json_keys传回执行json字段最上一层的key值 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
mysql>
select
json_keys(
'{"name":"yeyz","score":100}'
);
+
------------------------------------------+
| json_keys(
'{"name":"yeyz","score":100}'
) |
+
------------------------------------------+
| [
"name"
,
"score"
] |
+
------------------------------------------+
1 row
in
set
(0.01 sec)
mysql>
select
json_keys(
'{"name":"yeyz","score":{"math":100,"English":95}}'
);
+
----------------------------------------------------------------+
| json_keys(
'{"name":"yeyz","score":{"math":100,"English":95}}'
) |
+
----------------------------------------------------------------+
| [
"name"
,
"score"
] |
+
----------------------------------------------------------------+
1 row
in
set
(0.00 sec)
#如果有多层,可以在最后面使用$的方法,拿到其中的某一层的目录
mysql>
select
json_keys(
'{"name":"yeyz","score":{"math":100,"English":95}}'
,
'$.score'
);
+
--------------------------------------------------------------------------+
| json_keys(
'{"name":"yeyz","score":{"math":100,"English":95}}'
,
'$.score'
) |
+
--------------------------------------------------------------------------+
| [
"math"
,
"English"
] |
+
--------------------------------------------------------------------------+
1 row
in
set
(0.00 sec)
|
c、json_length函数,返回最上一层的key个数,如果想取到中间的某一层,则可以使用$的方法,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql>
select
json_length(
'{"name":"yeyz","score":{"math":100,"English":95},"age":26}'
);
+
---------------------------------------------------------------------------+
| json_length(
'{"name":"yeyz","score":{"math":100,"English":95},"age":26}'
) |
+
---------------------------------------------------------------------------+
| 3 |
+
---------------------------------------------------------------------------+
1 row
in
set
(0.00 sec)
mysql>
select
json_length(
'{"name":"yeyz","score":{"math":100,"English":95},"age":26}'
,
'$.score'
);
+
-------------------------------------------------------------------------------------+
| json_length(
'{"name":"yeyz","score":{"math":100,"English":95},"age":26}'
,
'$.score'
) |
+
-------------------------------------------------------------------------------------+
| 2 |
+
-------------------------------------------------------------------------------------+
1 row
in
set
(0.00 sec)
|
d、json_depth函数,json文件的深度,测试例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql>
select
json_depth(
'{"aaa":1}'
),json_depth(
'{}'
);
+
-------------------------+------------------+
| json_depth(
'{"aaa":1}'
) | json_depth(
'{}'
) |
+
-------------------------+------------------+
| 2 | 1 |
+
-------------------------+------------------+
1 row
in
set
(0.00 sec)
mysql>
select
json_depth(
'{"name":"yeyz","score":{"math":100,"English":95},"age":26}'
);
+
--------------------------------------------------------------------------+
| json_depth(
'{"name":"yeyz","score":{"math":100,"English":95},"age":26}'
) |
+
--------------------------------------------------------------------------+
| 3 |
+
--------------------------------------------------------------------------+
1 row
in
set
(0.00 sec)
|
这里需要注意的是,形如{'aa':1}这种形式的json,其深度是2 。
e、json_contains_path函数检索json中是否有一个或者多个成员.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
mysql>
set
@j=
'{"a":1,"b":2,"c":{"d":4}}'
;
Query OK, 0
rows
affected (0.00 sec)
#one的意思是只要包含一个成员,就返回1
mysql>
select
json_contains_path(@j,
'one'
,
'$.a'
,
'$.e'
);
+
------------------------------------------+
| json_contains_path(@j,
'one'
,
'$.a'
,
'$.e'
) |
+
------------------------------------------+
| 1 |
+
------------------------------------------+
1 row
in
set
(0.00 sec)
#
all
的意思是所有的成员都包含,才返回1
mysql>
select
json_contains_path(@j,
'all'
,
'$.a'
,
'$.e'
);
+
------------------------------------------+
| json_contains_path(@j,
'all'
,
'$.a'
,
'$.e'
) |
+
------------------------------------------+
| 0 |
+
------------------------------------------+
1 row
in
set
(0.01 sec)
mysql>
select
json_contains_path(@j,
'one'
,
'$.c.d'
);
+
--------------------------------------+
| json_contains_path(@j,
'one'
,
'$.c.d'
) |
+
--------------------------------------+
| 1 |
+
--------------------------------------+
1 row
in
set
(0.00 sec)
mysql>
select
json_contains_path(@j,
'one'
,
'$.a.d'
);
+
--------------------------------------+
| json_contains_path(@j,
'one'
,
'$.a.d'
) |
+
--------------------------------------+
| 0 |
+
--------------------------------------+
1 row
in
set
(0.00 sec)
|
f、json_type函数,判断json中的成员的类型,需要和json_extract结合起来使用.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
mysql>
select
*
from
test1;
+
------+---------------------------------+
| id | info |
+
------+---------------------------------+
| 1 | {
"age"
: 26,
"name"
:
"yeyz"
} |
| 2 | {
"age"
: 30,
"name"
:
"zhangsan"
} |
| 3 | {
"age"
: 35,
"name"
:
"lisi"
} |
+
------+---------------------------------+
3
rows
in
set
(0.00 sec)
#判断
name
的类型
mysql>
select
json_type(json_extract(info,
"$.name"
))
from
test1;
+
----------------------------------------+
| json_type(json_extract(info,
"$.name"
)) |
+
----------------------------------------+
| STRING |
| STRING |
| STRING |
+
----------------------------------------+
3
rows
in
set
(0.00 sec)
#判断age的类型
mysql>
select
json_type(json_extract(info,
"$.age"
))
from
test1;
+
---------------------------------------+
| json_type(json_extract(info,
"$.age"
)) |
+
---------------------------------------+
|
INTEGER
|
|
INTEGER
|
|
INTEGER
|
+
---------------------------------------+
3
rows
in
set
(0.00 sec)
#判断
name
和age组合起来的类型,可以看到是array
mysql>
select
json_type(json_extract(info,
"$.name"
,
"$.age"
))
from
test1;
+
------------------------------------------------+
| json_type(json_extract(info,
"$.name"
,
"$.age"
)) |
+
------------------------------------------------+
| ARRAY |
| ARRAY |
| ARRAY |
+
------------------------------------------------+
3
rows
in
set
(0.00 sec)
|
g、*的作用,所有的值,看下面的例子.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
{
"a"
:1,
"b"
:2,
"c"
:
{
"d"
:4
}
"e"
:
{
"d"
:
{
"ddd"
:
"5"
}
}
}
mysql>
set
@j=
'{"a":1,"b":2,"c":{"d":4},"e":{"d":{"ddd":"5"}}}'
;
Query OK, 0
rows
affected (0.00 sec)
#所有成员
mysql>
select
json_extract(@j,
'$.*'
);
+
---------------------------------------+
| json_extract(@j,
'$.*'
) |
+
---------------------------------------+
| [1, 2, {
"d"
: 4}, {
"d"
: {
"ddd"
:
"5"
}}] |
+
---------------------------------------+
1 row
in
set
(0.00 sec)
#所有成员中的d成员
mysql>
select
json_extract(@j,
'$.*.d'
);
+
--------------------------+
| json_extract(@j,
'$.*.d'
) |
+
--------------------------+
| [4, {
"ddd"
:
"5"
}] |
+
--------------------------+
1 row
in
set
(0.00 sec)
|
以上就是MySQL中json字段的操作方法的详细内容,更多关于MySQL json字段的资料请关注我其它相关文章! 。
原文链接:https://cloud.tencent.com/developer/article/1558311 。
最后此篇关于MySQL中json字段的操作方法的文章就讲到这里了,如果你想了解更多关于MySQL中json字段的操作方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!