转自:https://blog.csdn.net/qq_21187515/article/details/90760337/
其他:https://blog.csdn.net/nbskycity/article/details/107426735
其他:https://www.copylian.com/technology/440.html
eg: SELECT * FROM rec_monitor_device WHERE station_code = "IZP" AND device_category_code="0817" AND device_monitor->'$.JZGZ' = 'false'
对应的jpa写法:
@Query(value = "SELECT * FROM rec_monitor_device WHERE station_code = \"IZP\" AND (device_category_code=\"0821\" OR device_category_code=\"0819\") AND device_monitor->'$.GZ' = :statusFlag",nativeQuery = true)
最近用到了mysql5.7的json字段的检索查询,发现挺好用的,记录一下笔记
我们有一个日志表,里面的data字段是保存不同对象的json数据,遇到想根据里面的json的字段内容作为条件查询的情况
mysql根据json字段的内容检索查询数据
-
使用 字段->'$.json属性'进行查询条件
-
使用json_extract函数查询,json_extract(字段,"$.json属性")
-
根据json数组查询,用JSON_CONTAINS(字段,JSON_OBJECT('json属性', "内容"))
mysql5.7以上支持json的操作,以及增加了json存储类型
一般数据库存储json类型的数据会用json类型或者text类型
注意:用json类型的话
1)JSON列存储的必须是JSON格式数据,否则会报错。
2)JSON数据类型是没有默认值的。
查询根据表字段的json内容进行查询
- 首先创建表
create table log(
id int not null,
content varchar(255),
createTime TIMESTAMP,
data text
)
- 插入几个测试数据
- 执行查询sql
使用 字段->’$.json属性’进行查询条件
select * from log where data->'$.id' = 142;
或
select data->'$.id' id,data->'$.name' name from log where data->'$.id' = 142;
测试根据json数组的字段查询
1.再建一个表log2,插入几条json数组数据
[{
"id": "141",
"name": "xxx",
"type": "input"
},
{
"id": "142",
"name": "xin",
"type": "textarea"
}
]
查询json数组里面对象的id等于142的记录
用JSON_CONTAINS(字段,JSON_OBJECT(‘json属性’, “内容”))
select * from log2 where JSON_CONTAINS(data,JSON_OBJECT('id', "142"))
参考:
Simon格子的MySQL 5.7新增对JSON支持
mysql查询字段类型为json时的两种查询方式
09-09
主要介绍了mysql查询字段类型为json时的两种查询方式,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
mysql json 数组 搜索,如何使用JSON_SEARCH()在MySQL JSON数组中查找数字的路径?
--------------------------------------------------------------------------------------------------------------------------------
MySql查询JSON类型字段中某个属性值
A表中有如下数据:
id | user |
---|---|
1 | {"age": "15", "name": "尚书雨"} |
2 | {"age": "18", "name": "郭雨桐"} |
如果需要查询id为1的记录中user字段中age属性的值,则SQL语句是这样的:
select id, user -> '$.age' as age from A;
id | age |
---|---|
1 | 15 |
我的例子:
-- 查出0007设备中的三个温度:
SELECT device_monitor -> '$.YCDCKTXFWD' as YCDCKTXFWD, device_monitor -> '$.YCDCKTSFWD' as YCDCKTSFWD, device_monitor -> '$.YCDCKTHFWD' as YCDCKTHFWD FROM rec_monitor_device WHERE station_code = "IZP" AND device_category_code="0817" and device_unique = "P_IZP_KTJZ_ID0007" ;
-- 查出0008设备中的三个温度:
SELECT device_monitor -> '$.YCXCKTXFWD' as YCXCKTXFWD, device_monitor -> '$.YCXCKTSFWD' as YCXCKTSFWD, device_monitor -> '$.YCXCKTHFWD' as YCXCKTHFWD FROM rec_monitor_device WHERE station_code = "IZP" AND device_category_code="0817" and device_unique = "P_IZP_KTJZ_ID0008" ;
dao层:
//查出0007虚拟设备的三个温度
@Query(value = "SELECT device_monitor -> '$.YCDCKTXFWD' as YCDCKTXFWD FROM rec_monitor_device WHERE station_code = \"IZP\" AND device_category_code=\"0817\" and device_unique = \"P_IZP_KTJZ_ID0007\" ;",nativeQuery = true)
void get0007ThreeTemperture();
//查出0008虚拟设备的三个温度
@Query(value = "SELECT device_monitor -> '$.YCDCKTXFWD' as YCDCKTXFWD FROM rec_monitor_device WHERE station_code = \"IZP\" AND device_category_code=\"0817\" and device_unique = \"P_IZP_KTJZ_ID0007\" ;",nativeQuery = true)
void get0008ThreeTemperture();
SELECT device_monitor -> '$.YCDCKTXFWD' as YCDCKTXFWD, device_monitor -> '$.YCDCKTSFWD' as YCDCKTSFWD, device_monitor -> '$.YCDCKTHFWD' as YCDCKTHFWD FROM rec_monitor_device WHERE station_code = "IZP" AND device_category_code="0817" and device_unique = "P_IZP_KTJZ_ID0007" ;
-- 查出0008设备中的三个温度:
SELECT device_monitor -> '$.YCXCKTXFWD' as YCXCKTXFWD, device_monitor -> '$.YCXCKTSFWD' as YCXCKTSFWD, device_monitor -> '$.YCXCKTHFWD' as YCXCKTHFWD FROM rec_monitor_device WHERE station_code = "IZP" AND device_category_code="0817" and device_unique = "P_IZP_KTJZ_ID0008" ;
dao层:
//查出0007虚拟设备的三个温度
@Query(value = "SELECT device_monitor -> '$.YCDCKTXFWD' as YCDCKTXFWD FROM rec_monitor_device WHERE station_code = \"IZP\" AND device_category_code=\"0817\" and device_unique = \"P_IZP_KTJZ_ID0007\" ;",nativeQuery = true)
void get0007ThreeTemperture();
//查出0008虚拟设备的三个温度
@Query(value = "SELECT device_monitor -> '$.YCDCKTXFWD' as YCDCKTXFWD FROM rec_monitor_device WHERE station_code = \"IZP\" AND device_category_code=\"0817\" and device_unique = \"P_IZP_KTJZ_ID0007\" ;",nativeQuery = true)
void get0008ThreeTemperture();