file_type
可以是 varchar
,也可以是 json
类型
JSON_CONTAINS(json_doc, val[, path])
:判断是否包含某个json值
JSON_ARRAY([val[, val] ...])
:创建json数组
{"key": 1, "name": "万飞"}
查询
SELECT * FROM `ak_file_config` where file_type -> '$.name' = "万飞"
["EXE", "白加黑", "DLL"]
查询
SELECT * FROM `ak_file_config` where JSON_CONTAINS(file_type, JSON_ARRAY("白加黑","DLL"))
SELECT * FROM `ak_file_config` where JSON_CONTAINS(file_type,'"DLL"') OR JSON_CONTAINS(file_type,'"EXE"')
参考 https://wenku.baidu.com/view/0831b7cc6194dd88d0d233d4b14e852459fb3958?aggId=0831b7cc6194dd88d0d233d4b14e852459fb3958
// jsonArray查询
.apply(CollUtil.isNotEmpty(query.getFileType()), StrUtil.format("JSON_CONTAINS(t.file_type, JSON_ARRAY({}))",
// 设置占位符{0},{1},{2}
IntStream.range(0, Optional.ofNullable(query.getFileType()).orElse(Collections.emptyList()).size())
.mapToObj(i -> "{".concat(String.valueOf(i)).concat("}"))
.collect(Collectors.joining(","))),
Optional.ofNullable(query.getFileType()).orElse(Collections.emptyList()).toArray())
参考 https://blog.csdn.net/qq_31832209/article/details/125374325
SELECT
*
FROM
`ak_file_config`
WHERE
JSON_EXTRACT(file_type, '$' ) LIKE '%DL%';
String productOrCompanyName = query.getProductOrCompanyName().replace("\"", "_");
.and(StrUtil.isNotBlank(productOrCompanyName),
wq -> wq.apply("JSON_EXTRACT(LOWER(t.label_involve_product), '$') LIKE LOWER(CONCAT('%', {0}, '%'))", productOrCompanyName)
.or()
.apply("JSON_EXTRACT(LOWER(t.label_involve_company), '$') LIKE LOWER(CONCAT('%', {0}, '%'))", productOrCompanyName))
如果输入字符串带双引号,需要将
\"
替换成_
进行模糊搜索,但是会查询出不带双引号的数据
mysql
最低版本8.0.4
SELECT
*
FROM
JSON_TABLE ( '["11", "22"]', '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result INT PATH '$' ) ) ) AS t;
json
对象数组参考 https://cdn.modb.pro/db/484630
SELECT
t1.result,
count( t1.result ) AS count
FROM
pe_main_body t
INNER JOIN JSON_TABLE ( t.overview_product_type, '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result VARCHAR ( 100 ) PATH '$' ) ) ) AS t1
WHERE
del_flag = FALSE
GROUP BY
t1.result
例如:match_context
字段是对象数组,对象里面有两个字段keyWord
和describe
需求:查询出所有的不重复的对象
SELECT DISTINCT
t1.result
FROM
ii_sensitive_resource_info t
INNER JOIN JSON_TABLE ( t.match_context, '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result JSON PATH '$' ) ) ) AS t1
WHERE
company_id IN ( 296 )
DISTINCT
:去重
SELECT
result ->> '$.keyWord' AS keyWord,
result ->> '$.describe' AS descInfo
FROM
(
SELECT DISTINCT
t1.result
FROM
ii_sensitive_resource_info t
INNER JOIN JSON_TABLE ( t.match_context, '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result JSON PATH '$' ) ) ) AS t1
WHERE
company_id IN ( 296 )) tmp
->>
:会去除双引号
JSONArray
符号是$[0]
JSONObject
符号是$
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE `address`->'$[0].name' LIKE "%b%"
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE `address`->'$[0].name' = "bbb"
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE CAST(JSON_UNQUOTE(`address`->'$[0].date') AS DATETIME) BETWEEN '2023-08-13' AND '2023-08-17'
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE `address`->'$[0].name' IN ("bbb","ccc")
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE json_contains(`address`, '["bbb"]', "$[0].nickname")