腾讯云微搭低代码 实现多对多关联查询

这篇说说腾讯云数据模型中多对多关联字段的筛选方法,支持通过关联表字段作为查询条件,仅MySQL数据库支持此功能,并以学生与课程表为例展示APIs自定义查询配置。 关联表筛选:通过relateWhere参数指定关联字段及查询条件。 示例一:查询选课包含“语文”的学生,返回张三和李四。 示例二:查询张三的“英文”课选课记录,仅返回一条结果。

相关产品
腾讯云微搭低代码 产品页,含版本规格、价格与购买入口。

多对多字段本身不能作为条件筛选,但是支持选择多对多关联表内的字段作为筛选条件。

注意:仅 MySQL 数据库支持多对多关联关系,如你的底层不是 MySQL 数据库,则无法创建。

你可以在对应模型的基础信息中查看数据库类型。

一、数据模型

学生表和课程表之间存在多对多关联关系,学生表中关联字段为学生选课(xsxk)。

学生表:

关联关系配置:

课程表:

关联关系配置:

二、查询条件

下面以 APIs 自定义方法为例,介绍如何配置查询条件,该查询以学生表为主表,课程表为关联表。

三、查询选课中包含语文课的学生名单

查询条件:

module.exports = async function (params, context) { const result = await context.callModel({ name: 'xsb_rx0zqhc', // 数据模型标识,可以前往「数据源 – 数据模型」列表页查看 methodName: 'wedaGetRecordsV2', // 数据模型方法标识 params: { filter: { where: { }, relateWhere:{ // 关联表查询 xsxk:{ // 学生表中的关联字段 where:{ kcmc:{ // 课程表中需要查询的字段名称 $in: ["语文"] } } } } }, // 排序 orderBy: [ { createdAt: "desc", // 创建时间,倒序 }, ], // 返回字段选择 select: { $master: true, // 返回主表中的字段 xsxk: true // 返回关联字段内容 }, // 返回total字段 getCount: true, // 页面大小 pageSize: 10, // 当前页面 pageNumber: 1, }, }); // 在这里返回这个方法的结果,需要与出参定义的结构映射 return result;};

查询结果:

{ "total": 2, "records": [{ "owner": "1534736158943625218", "createdAt": 1694070086572, "createBy": "1534736158943625218", "updateBy": "1534736158943625218", "xsxk": [{ "owner": "1534736158943625218", "createdAt": 1693972871239, "createBy": "1534736158943625218", "updateBy": "1534736158943625218", "_id": "7UF1G8XKK6", "kcmc": "语文", "updatedAt": 1693972871239 }], "xsxm": "张三", "_id": "7UNM87D3FW", "updatedAt": 1694070086572 }, { "owner": "1534736158943625218", "createdAt": 1693972984077, "createBy": "1534736158943625218", "updateBy": "1534736158943625218", "xsxk": [{ "owner": "1534736158943625218", "createdAt": 1693972871239, "createBy": "1534736158943625218", "updateBy": "1534736158943625218", "_id": "7UF1G8XKK6", "kcmc": "语文", "updatedAt": 1693972871239 }], "xsxm": "李四", "_id": "7UF1SESBCC", "updatedAt": 1693972984077 } ]}

四、查询张三英语课的选课记录

查询条件:

module.exports = async function (params, context) { const result = await context.callModel({ name: 'xsb_rx0zqhc', // 数据模型标识,可以前往「数据源 – 数据模型」列表页查看 methodName: 'wedaGetRecordsV2', // 数据模型方法标识 params: { filter: { where: { // 主表查询 $and: [{ xsxm: { // 学生表中需要查询的字段名称 $eq: "张三" } }] }, relateWhere:{ // 关联表查询 xsxk:{ // 学生表中的关联字段 where:{ kcmc:{ // 课程表中需要查询的字段名称 $in: ["英文"] } } } } }, // 排序 orderBy: [ { createdAt: "desc", // 创建时间,倒序 }, ], // 返回字段选择 select: { $master: true, // 返回主表中的字段 xsxk: true // 返回关联字段内容 }, // 返回total字段 getCount: true, // 页面大小 pageSize: 10, // 当前页面 pageNumber: 1, }, }); // 在这里返回这个方法的结果,需要与出参定义的结构映射 return result;};

查询结果:

{ "total": 1, "records": [{ "owner": "1534736158943625218", "createdAt": 1694072317951, "createBy": "1534736158943625218", "updateBy": "1534736158943625218", "xsxk": [{ "owner": "1534736158943625218", "createdAt": 1693972887373, "createBy": "1534736158943625218", "updateBy": "1534736158943625218", "_id": "7UF1HQD1QL", "kcmc": "英文", "updatedAt": 1693972887373 }], "xsxm": "张三", "_id": "7UNT5CS91A", "updatedAt": 1694072317951 }]}

赞 (0)
上一篇 4天前
下一篇 4天前