当前位置:网站首页>MySQL学习笔记:JSON嵌套数组查询
MySQL学习笔记:JSON嵌套数组查询
2022-06-11 02:40:00 【山鬼谣me】
环境
MySQL 5.7
先假设数据
简单JSON串就不说了,很简单,来个复杂点的:
[
{
"type":[
{
"code":"xmf",
"name":"小蜜蜂"
}
],
"cities":[
{
"code":"1",
"name":"上海"
}
],
"oType":[
{
"code":"order",
"name":"单子"
}
],
"p":[
{
"code":"110",
"name":"快点送"
}
]
}
]
在MySQL数据库中,它长这个样子:

上图只是为了说明,上面整个JSON串在数据库里对应的字段是detail。
网上很多都是先{......}对象,然后再嵌套数组,但是我的数据是[......],即:先是个数组,然后嵌套对象,然后再嵌套数组。
这种情况下,该如何查询呢?
MySQL 嵌套数组查询方法
现在想查询,JSON串中城市字段是上海的。
正确的SQL:
SELECT *
from `template_yutao`
where json_contains( `detail`, json_object('cities', json_array(json_object('name', '上海'))))
思路:json_contains作用:是否包含子文档。那我们就构造一个子文档出来,然后输入进去查询即可。
我曾尝试使用提取方法,想简写上面的SQL:
-- 想着先提取cities这一层,再进行查询
-- 很遗憾,在json_contains里面不能写 * 或 **
SELECT * from `alsc_cs_minos_template_biz`
where json_contains( `detail`, json_object('name', '上海'), '$[*].cities')
-- 之后又改写成 下面这个可以成功,但是$[0]等于限制成了数组的第一个,不方便以后扩展。
-- 研究了半天,没有研究出提取一层的方法;不过上面的查询已经够用了。
--其实因为json_contains里面不能写 * 或 **,就已经说明它其实不能提取,只能明确指定提取。
SELECT * from `alsc_cs_minos_template_biz`
where json_contains( `detail`, json_object('name', '上海'), '$[0].cities')
说明:
这里需要说下,上面的json查询,和MongoDB相似,但是却没有MongoDB强大,MongoDB的话,只需要这么查:
SELECT *
from `template_yutao`
where detail.cities.name = '上海' -- 这不是标准的MongoDB写法,只是想表达大概的意思。
-- MongoDB标准写法
db.template_yutao.find('$.detail.cities.name', '上海');
Mybatis 结合
假设我们利用Mybatis-Generator生成Mapper文件中的代码是:criteria 类的形式。
Mapper.xml形如:
<if test="_parameter != null">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and t.${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and t.${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and t.${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</if>
<if test="orderByClause != null">
order by ${@[email protected](orderByClause)}
</if>
<if test="page">
limit #{pageIndex},#{pageSize}
</if>
相关类XXXParam类,形如下面的形式:
protected abstract static class AbstractGeneratedCriteria {
protected List<Criterion> criteria;
protected AbstractGeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andIdIsNull() {
addCriterion("id is null");
return (Criteria) this;
}
public Criteria andIdIsNotNull() {
addCriterion("id is not null");
return (Criteria) this;
}
那么这时,代码该如何编写呢?
我的做法就是在里面添加一个如下方法:
/** * * @param cityList * @return */
public Criteria andCityCodeJson(List<String> cityList) {
if (cityList == null || cityList.size() == 0) {
throw new RuntimeException("Value for cityList cannot be null");
}
StringBuilder cityStr = new StringBuilder();
int size = cityList.size();
for (int i = 0; i < size; i++) {
if (i == size - 1) {
cityStr.append("json_object('code', '").append(cityList.get(i)).append("')");
} else {
cityStr.append("json_object('code', '").append(cityList.get(i)).append("')").append(",");
}
}
addCriterion("json_contains(detail, json_object('city', json_array(" + cityStr +")))");
return (Criteria) this;
}
MySQL JSON方法说明
以下是简单说明,言简意赅的那种,详情可以参考官网:
12.18.3 Functions That Search JSON Values
json_contains
JSON_CONTAINS(json_doc, val[, path]) 是否包含子文档.
JSON_EXTRACT
JSON_EXTRACT(json_doc, path[, path] …) 获得doc中某个或多个节点的值。
参考地址
边栏推荐
- 码农的进阶之路 | 每日趣闻
- Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
- Graphacademy course explanation: Fundamentals of neo4j graph data science
- Baidu submits sitemap to prompt the solution of "index type is not handled"
- . Net module and assembly - NET Module vs Assembly
- Array full permutation
- Minimum common ancestor of binary tree
- [189. rotation array]
- CPT 102_LEC 17
- AOSP ~ 修改WebView默认实现
猜你喜欢

CocosCreator原生二次开发的正确姿势

Manon's advanced road - Daily anecdotes

Three special data types, day3 and redis (geographic location, cardinality statistics and bitmap scene usage)

Why did those who left Beijing, Shanghai and Guangzhou with a smile cry in the end?

Blue Bridge Cup_ Xiao Lan eats candy_ Pigeon nest principle / drawer principle

Cygwin reports an error child_ info_ fork::abort: XXX. dll: Loaded to different address: parent(XXX) != child(XXX)

RS232/RS485转4G DTU 上传基于Modbus协议的温湿度传感器数据到远程TCP服务器

那些笑着离开“北上广”的人,为何最后都哭了?
![[189. rotation array]](/img/cc/0da616ad9adc9c9d352e54f58dbe41.png)
[189. rotation array]

Baidu submits sitemap to prompt the solution of "index type is not handled"
随机推荐
CPT 102_LEC 20
Android WiFi hide SSID
RS232/RS485转4G DTU 上传基于Modbus协议的温湿度传感器数据到远程TCP服务器
How to handle error code 30204-44 when installing office 2016 in win10?
[new open source project] dynamic configuration task scheduling framework gobrs async joins the dromara open source community
Graphacademy course explanation: Fundamentals of neo4j graph data science
Stringutils string tool class used by FreeMarker to create templates
Will your company choose to develop data center?
If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas
银行选择电子招标采购的必要性
完成千万元A轮融资,小象生活能否成为折扣界的“永辉”?
ORA-00392 ORA-00312 错误处理
jdbc工具類的問題
Arduino uses nRF24L01 module for wireless communication
Array full permutation
二叉树最小最低公共祖先
CPT 102_LEC 15
[implementation of bubble sorting]
Go quick start of go language (I): the first go program
CPT 102_LEC 18