Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Report] 怎么对关联查询字段进行模糊匹配达到模糊关联的效果 #578

Open
ForJ-W opened this issue May 29, 2023 · 5 comments

Comments

@ForJ-W
Copy link

ForJ-W commented May 29, 2023

APIJSON Version/APIJSON 版本号

6.0.0

Database Type & Version/数据库类型及版本号

msyql8.0

Environment/环境信息

- JDK/基础库:1.8
- OS/系统:win11

APIAuto Screenshots/APIAuto 请求与结果完整截屏

评论传

Current Behavior/问题描述

怎么在对关联查询字段进行模糊匹配

Expected Behavior/期望结果

能够查询出模糊匹配的关联字段查询

Any additional comments?/其它补充说明?

No response

@ForJ-W
Copy link
Author

ForJ-W commented May 29, 2023

image
改成
image

想要的结果就是不仅相等的82001能够查询,还能查询出182001 820010此类数据,但是目前没有找到相关查询方式

@ForJ-W
Copy link
Author

ForJ-W commented May 29, 2023

请问怎么支持此类查询语法,为查询到相关内容;
image
image

@ForJ-W ForJ-W changed the title [Report] [Report] 怎么对关联查询字段进行模糊匹配达到模糊关联的效果 May 29, 2023
@TommyLemon
Copy link
Collaborator

TommyLemon commented Jun 2, 2023

不支持你截屏那样拼接,你把被关联引用字段的值提前拼接成这样就行了,
可以用 SQL 函数
"@column": "concat('%',id,'%'):id"

或 APIJSON 远程函数
"id()": "wrapPercent(id)" // 假设你写了这个远程函数,return "%" + idVal + "%"

然后要用模糊搜索的关键词,看通用文档!
"userId$@": "User/id"

#36

@TommyLemon
Copy link
Collaborator

TommyLemon commented Jun 3, 2023

还可以这样

"content%$@": "/User/name"

"content%_$@": "/User/name"

"content_?$@": "/User/name"
image

http://apijson.cn/api/?type=JSON&json=%7B%22User%22%3A%7B%22%40column%22%3A%22id%2Cname%22%2C%22id%22%3A38710%7D%2C%22Moment%22%3A%7B%22content%25%24%40%22%3A%22%2FUser%2Fname%22%2C%22%40explain%22%3Atrue%7D%7D%0A

具体见

/**WHERE key LIKE 'value'
* @param key
* @param column
* @param value
* @return key LIKE 'value'
*/
@JSONField(serialize = false)
public String getLikeString(@NotNull String key, @NotNull String column, String value) {
String k = key.substring(0, key.length() - 1);
char r = k.charAt(k.length() - 1);
char l;
if (r == '%' || r == '_' || r == '?') {
k = k.substring(0, k.length() - 1);
l = k.charAt(k.length() - 1);
if (l == '%' || l == '_' || l == '?') {
if (l == r) {
throw new IllegalArgumentException(key + ":value 中字符 " + k + " 不合法!key$:value 中不允许 key 中有连续相同的占位符!");
}
k = k.substring(0, k.length() - 1);
}
else if (l > 0 && StringUtil.isName(String.valueOf(l))) {
l = r;
}
if (l == '?') {
l = 0;
}
if (r == '?') {
r = 0;
}
}
else {
l = r = 0;
}
if (l > 0 || r > 0) {
if (value == null) {
throw new IllegalArgumentException(key + ":value 中 value 为 null!key$:value 中 value 不能为 null,且类型必须是 String !");
}
value = value.replaceAll("\\\\", "\\\\\\\\");
value = value.replaceAll("\\%", "\\\\%");
value = value.replaceAll("\\_", "\\\\_");
if (l > 0) {
value = l + value;
}
if (r > 0) {
value = value + r;
}
}
return getKey(column) + " LIKE " + getValue(key, column, value);
}

image

@TommyLemon
Copy link
Collaborator

自己多看文档多尝试,我现在在 GitHub 上可能一周只能回你一次消息,
如果需要及时的回复,请登记贵公司,加企业支持专群
#187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants