一、版本要求
MySql4.7
二、字段类型:
json类型
三、需要虚拟索引
关于需要处理的json字段,创建虚拟索引:
ALTER TABLE wxhk.t_iotdata ADD sn_virtual varchar (32) GENERATED ALWAYS AS (json_extract(DataContent, ‘$.serialNumber’ )) VIRTUAL;
四、查询:
如果查找内容是一介字段只需要前加‘
.
’
s
e
l
e
c
t
j
s
o
n
e
x
t
r
a
c
t
(
D
a
t
a
C
o
n
t
e
n
t
,
′
.’ select json_extract(DataContent,'
.’selectjsonextract(DataContent,′.deviceId’) from wxhk.t_iotdata where json_extract(DataContent,’
.
d
e
v
i
c
e
I
d
′
)
=
"
x
x
x
"
;
如
果
查
找
内
容
是
多
阶
子
字
段
,
需
要
‘
.deviceId')="xxx"; 如果查找内容是多阶子字段,需要‘
.deviceId′)="xxx";如果查找内容是多阶子字段,需要‘.’后加前面各介父字段名,如下:
select json_extract(DataContent,’
.
d
e
v
i
c
e
I
n
f
o
.
m
a
c
′
)
f
r
o
m
w
x
h
k
.
t
i
o
t
d
a
t
a
w
h
e
r
e
j
s
o
n
e
x
t
r
a
c
t
(
D
a
t
a
C
o
n
t
e
n
t
,
′
.deviceInfo.mac') from wxhk.t_iotdata where json_extract(DataContent,'
.deviceInfo.mac′)fromwxhk.tiotdatawherejsonextract(DataContent,′.deviceInfo.mac’)=“xxx”;