APIJSON icon indicating copy to clipboard operation
APIJSON copied to clipboard

Mysql 中 json 类型的 数据怎么操作呢?

Open 157677678 opened this issue 2 years ago • 10 comments

数据库中 有个字段是json 类型(字段名称: other 值: {"type":"1"} ) 直接写sql 的话 我都是 通过other->'$.type' 来操作 现在key不能 这么写 。那我应该怎么操作最合理呢?

另外 我试图用@raw 解决 不过没明白 @raw使用方法 这里有更详细的文档么?
我在apijsonboot 这个项目 看到 可以在 RAW_MAP 中定义,我在后台定义了 RAW_MAP.put("other.type", "other-> '$.type'"); 可是前台请求应该怎么写才能生成 other->'$.type' =1 这种sql呢?

157677678 avatar Aug 22 '22 09:08 157677678

WHERE/HAVING + json 函数: https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

"other{}": "json_contains(other,'1','$.type')"
"@having": "json_contains(other,'1','$.type')"

APIJSON 默认支持 400 个左右 SQL 函数,可自己加 https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java#L265-L728

TommyLemon avatar Aug 24 '22 13:08 TommyLemon

感谢指导 如果方便 能再指点一下 @raw 的使用吗?或者有具体例子我学习一下 谢谢

157677678 avatar Aug 30 '22 01:08 157677678

https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLConfig.java#L100-L110

另外 issue 里搜 raw https://github.com/Tencent/APIJSON/issues?q=is%3Aissue+raw

https://github.com/Tencent/APIJSON/issues/165

TommyLemon avatar Aug 30 '22 15:08 TommyLemon

从文档里看 row 更多用在select 后面 或者 having中 能用在 where 条件吗? 特别是条件包含动态信息? 还是只能用function去处理呢?或者用 combine

157677678 avatar Aug 31 '22 04:08 157677678

l

如果 用"other{}": "json_extract(other,'$.type')=1"这个形式 咱们不支持是么?

157677678 avatar Aug 31 '22 08:08 157677678

l

如果 用"other{}": "json_extract(other,'$.type')=1"这个形式 咱们不支持是么?

这样可以用模糊查询 或者还有什么方式可以对json的字段进行模糊查询呢?

157677678 avatar Aug 31 '22 08:08 157677678

支持

TommyLemon avatar Sep 03 '22 15:09 TommyLemon

嗯 "other{}": "json_extract(other,'$.type')=1" 这个支持 我还有个字段是 string
"other{}": "json_extract(other,'$.info') = '类型一'" "other{}": "json_extract(other,'$.info') like '类型一'" "other{}": "json_extract(other,'$.info') like '%一%'" 都不行 提示错误 预编译模式下 other{}:\"column?value;function(arg0,arg1,...)?value...\" 中 ?value 必须符合正则表达式 ^[0-9%,!=\\<\\>/\\.\\+\\-\\*\\^]+$ 且不包含连续减号 -- 或注释符 /* !不允许多余的空格! 我理解是 咱们设定value 必须是数字 如果像我这种必须是字符串 该怎么处理呢? 特别是如果使用 like 这种模糊查询

157677678 avatar Sep 05 '22 06:09 157677678

用 @raw 指定 key

"other{}": "json_extract(other,'$.info') = '类型一'"  // 对应 RAW_MAP.put("json_extract(other,'$.info') = '类型一'", "");
"@raw": "other{}"

TommyLemon avatar Sep 06 '22 16:09 TommyLemon

感谢指导

157677678 avatar Sep 15 '22 08:09 157677678