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

多对多关联查询咨询 #753

Open
zxcwindy opened this issue Aug 12, 2024 · 2 comments
Open

多对多关联查询咨询 #753

zxcwindy opened this issue Aug 12, 2024 · 2 comments

Comments

@zxcwindy
Copy link
Contributor

zxcwindy commented Aug 12, 2024

Description

目前有三张表,分别是user、org(组织架构)、org_user。其中user和org是多对多关系,一个org可以对应多个user,一个user也可以对应多个org,他们之间的关联关系是通过org表记录的(org_user表字段为org_id和user_id)。

现在想实现一个一对多查询的效果,即查询每个用户和他对应的组织列表信息。

首先尝试如下图的查询请求
2024-08-12 17-26-57-1

返回的结果有重复,查看后端执行的sql,sql语句是拼写正常的
2024-08-12 17-25-14-2

我期望的结果是如下格式的

{
  "list": [
    {
      "user": {
        "userId": "1",
        "userCnName": "管理员"
      },
      "orgList": [
        {
          "orgId": "TestOrg",
          "orgCode": "TestOrg",
          "orgName": "测试组织"
        },
        {
          "orgId": "Org",
          "orgCode": "Org",
          "orgName": "集团组织"
        }
      ]
    }
  ],
  "ok": true,
  "code": 200,
  "msg": "success",
  "time": 1723453958196
}

或者

{
  "list": [
    {
      "user": {
        "userId": "1",
        "userCnName": "管理员",
        "orgList": [
          {
            "orgId": "TestOrg",
            "orgCode": "TestOrg",
            "orgName": "测试组织"
          },
          {
            "orgId": "Org",
            "orgCode": "Org",
            "orgName": "集团组织"
          }
        ]
      }
    }
  ],
  "ok": true,
  "code": 200,
  "msg": "success",
  "time": 1723453958196
}

又尝试过如下写法,但提示异常

2024-08-12 17-19-37-3

后面研究了下,尝试了下以下写法是可以的,但是会出现1 + N 的问题,比如我User表里面有4条记录,就一共会查询5次,这个很影响性能,有没写法可以只查询一次的写法

{
  "[]": {
    "User": {},
    "Org[]": {
      "join": "&/Org/org_id@",
      "OrgUser": {
	"user_id@": "[]/User/user_id"
      },
      "Org": {
	"org_id@": "/OrgUser/org_id"
      }
    }
  },
  "format": true
}
@TommyLemon
Copy link
Collaborator

APP JOIN 支持跨层级,不过不支持子数组内还有 join
https://github.com/Tencent/APIJSON/releases/tag/5.1.5

所以以上这个 1 + N (子数组内 join) 也只会变为 2 + N(子数组内单表查询)

@TommyLemon
Copy link
Collaborator

目前解析过程对 一对多、多对多 的 SQL JOIN 处理有 bug,如果不用 DISTINCT 或 GROUP BY 来去重,则会导致副表数据总是同一个,应该把现在 AbstractSQLExecutor 把结果集处理成副表单条简单 WHERE id=${id} 的查询 SQL 缓存,改为统一返回到 AbstractParser.onArrayParse 中,直接把数据赋值给副表对象,省去大量的副表解析 JSON 及执行 SQL 过程,解决以上问题

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