内容简介:用时4小时,调试加数据库设置,代码编写,查文档。允许我偷个懒,不想总结了,仔细阅读内容,基本上可以了解Sequelize在联表查询上的基本用法了
-
一对一
在controller里面如下写
// 获取学生信息 通过一对多的联系 async info(){ const { ctx, app } = this; let result = await app.model.Student.findAll({ include: { model: app.model.Info } }); ctx.body = result; } 复制代码
获取到的值如下:
[ // 第一个学生 { "id": 1, "number": "160101", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:16:09", "updatedAt": "2019-05-12 13:16:12", "deletedAt": null, "info": { // 联表查到的信息 "sex": "男", "id": 1, "name": "许仙", "age": 23, "studentId": 1, "createdAt": "2019-05-12 13:25:58", "updatedAt": "2019-05-12 13:26:01", "deletedAt": null } }, // 第二个学生 { "id": 2, "number": "160201", "password": "202cb962ac59075b964b07152d234b70", "classId": 2, "createdAt": "2019-05-12 13:16:32", "updatedAt": "2019-05-12 13:16:35", "deletedAt": null, "info": { "sex": "女", "id": 2, "name": "白素贞", "age": 20, "studentId": 2, "createdAt": "2019-05-12 13:26:41", "updatedAt": "2019-05-12 13:26:46", "deletedAt": null } }, { "id": 3, "number": "160102", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:17:17", "updatedAt": "2019-05-12 13:17:21", "deletedAt": null, "info": { "sex": "男", "id": 3, "name": "法海", "age": 22, "studentId": 3, "createdAt": "2019-05-12 13:27:20", "updatedAt": "2019-05-12 13:27:22", "deletedAt": null } }, { "id": 4, "number": "160103", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:17:51", "updatedAt": "2019-05-12 13:17:54", "deletedAt": null, "info": { "sex": "女", "id": 4, "name": "小青", "age": 18, "studentId": 4, "createdAt": "2019-05-12 13:27:48", "updatedAt": "2019-05-12 13:27:51", "deletedAt": null } }, { "id": 5, "number": "160104", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:18:13", "updatedAt": "2019-05-12 13:18:16", "deletedAt": null, "info": { "sex": "女", "id": 5, "name": "金如意", "age": 20, "studentId": 5, "createdAt": "2019-05-12 13:28:34", "updatedAt": "2019-05-12 13:28:37", "deletedAt": null } }, { "id": 6, "number": "160202", "password": "202cb962ac59075b964b07152d234b70", "classId": 2, "createdAt": "2019-05-12 13:18:36", "updatedAt": "2019-05-12 13:18:39", "deletedAt": null, "info": { "sex": "男", "id": 6, "name": "景松", "age": 23, "studentId": 6, "createdAt": "2019-05-12 13:30:07", "updatedAt": "2019-05-12 13:30:10", "deletedAt": null } } ] 复制代码
-
一对多
// 获取班级名为 软件工程1601 的班级学生 async student(){ const { ctx, app } = this; let result = await app.model.Classes.findAll({ where: { name: '软件工程1601' }, include: { model: app.model.Student } }) ctx.body = result; } 复制代码
获取数据如下:
[ { "id": 1, "name": "软件工程1601", "createdAt": "2019-05-12 13:11:43", "updatedAt": "2019-05-12 13:11:47", "deletedAt": null, "students": [ { "id": 1, "number": "160101", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:16:09", "updatedAt": "2019-05-12 13:16:12", "deletedAt": null }, { "id": 3, "number": "160102", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:17:17", "updatedAt": "2019-05-12 13:17:21", "deletedAt": null }, { "id": 4, "number": "160103", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:17:51", "updatedAt": "2019-05-12 13:17:54", "deletedAt": null }, { "id": 5, "number": "160104", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:18:13", "updatedAt": "2019-05-12 13:18:16", "deletedAt": null } ] } ] 复制代码
-
多对多
从学生获取课程信息
// 获取学生的选课内容 async lession(){ const { ctx, app } = this; let result = await app.model.Student.findAll({ where:{ id: 1, }, include: [ {model: app.model.Info}, {model: app.model.Lession} ] }); ctx.body = result; } 复制代码
这里的话,注意include的值为一个数组了,这样可以多个联表获取数据
数据如下:
[ { "id": 1, "number": "160101", "password": "202cb962ac59075b964b07152d234b70", "classId": 1, "createdAt": "2019-05-12 13:16:09", "updatedAt": "2019-05-12 13:16:12", "deletedAt": null, "info": { "sex": "男", "id": 1, "name": "许仙", "age": 23, "studentId": 1, "createdAt": "2019-05-12 13:25:58", "updatedAt": "2019-05-12 13:26:01", "deletedAt": null }, "lessions": [ { "id": 1, "name": "计算机网络", "createdAt": "2019-05-12 13:12:32", "updatedAt": "2019-05-12 13:12:35", "deletedAt": null, "lession_student": { "lessionId": 1, "studentId": 1, "createdAt": "2019-05-12 13:20:35", "updatedAt": "2019-05-12 13:20:40", "deletedAt": null } }, { "id": 2, "name": "Java程序设计", "createdAt": "2019-05-12 13:12:50", "updatedAt": "2019-05-12 13:12:52", "deletedAt": null, "lession_student": { "lessionId": 2, "studentId": 1, "createdAt": "2019-05-12 13:23:10", "updatedAt": "2019-05-12 13:23:13", "deletedAt": null } }, { "id": 3, "name": "软件项目管理", "createdAt": "2019-05-12 13:13:07", "updatedAt": "2019-05-12 13:13:10", "deletedAt": null, "lession_student": { "lessionId": 3, "studentId": 1, "createdAt": "2019-05-12 13:24:21", "updatedAt": "2019-05-12 13:24:24", "deletedAt": null } } ] } ] 复制代码
从课程获取选课学生:
// 获取某个课的参课学生 async lessionStudent(){ const { ctx, app } = this; let result = await app.model.Lession.findAll({ where:{ name: '网络安全' }, include: { model: app.model.Student, include: { model: app.model.Info } } }); ctx.body = result; } 复制代码
这里注意,在include的下面又有一个include,第二个include是相对Student表的
数据如下:
[ { "id": 4, "name": "网络安全", "createdAt": "2019-05-12 13:13:22", "updatedAt": "2019-05-12 13:13:25", "deletedAt": null, "students": [ { "id": 2, "number": "160201", "password": "202cb962ac59075b964b07152d234b70", "classId": 2, "createdAt": "2019-05-12 13:16:32", "updatedAt": "2019-05-12 13:16:35", "deletedAt": null, "lession_student": { "lessionId": 4, "studentId": 2, "createdAt": "2019-05-12 13:24:59", "updatedAt": "2019-05-12 13:25:03", "deletedAt": null }, "info": { "sex": "女", "id": 2, "name": "白素贞", "age": 20, "studentId": 2, "createdAt": "2019-05-12 13:26:41", "updatedAt": "2019-05-12 13:26:46", "deletedAt": null } }, { "id": 6, "number": "160202", "password": "202cb962ac59075b964b07152d234b70", "classId": 2, "createdAt": "2019-05-12 13:18:36", "updatedAt": "2019-05-12 13:18:39", "deletedAt": null, "lession_student": { "lessionId": 4, "studentId": 6, "createdAt": "2019-05-12 13:25:12", "updatedAt": "2019-05-12 13:25:15", "deletedAt": null }, "info": { "sex": "男", "id": 6, "name": "景松", "age": 23, "studentId": 6, "createdAt": "2019-05-12 13:30:07", "updatedAt": "2019-05-12 13:30:10", "deletedAt": null } } ] } ] 复制代码
4. 总结
用时4小时,调试加数据库设置,代码编写,查文档。允许我偷个懒,不想总结了,仔细阅读内容,基本上可以了解Sequelize在联表查询上的基本用法了
以上所述就是小编给大家介绍的《在EggJS中使用Sequelize做联表查询》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 使用Jest查询Elasticsearch
- 使用phoenix查询hbase
- 使用ProxySQL查询缓存进行扩展
- 使用Jest查询Elasticsearch数据
- 如何在django中使用子查询?
- 灵活使用 SQLAlchemy 中的 ORM 查询
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Java编程思想
埃克尔 / 机械工业出版社 / 2007-5-1 / 79.00元
《Java编程思想(英文版•第4版)》内容简介:特色:1.适合初学者与专业人员的经典的面向对象的叙述方式;为更新的Java SE5/6的相关内容增加了新的示例和章节。2.测验框架显示程序输出。3.设计模式贯穿于众多示例中:适配器、桥接器、职责链、命令、装饰器、外观、工厂方法、享元、点名、数据传输对象、空对象、代理、单例、状态、策略、模板方法以及访问者。4.为数据传输引入了XML;为用户界面引入了S......一起来看看 《Java编程思想》 这本书的介绍吧!