如何在纯 SQL 查询中将多对多结果作为列表
我在两个实体上有多对多关系:Student
和 Teacher
。他们的公共表是course
。每个学生都可以和老师一起上一门课程。这是表模式,我用初始测试值填充它们:
//Fill student table
db.execSQL("INSERT OR IGNORE INTO student_table (studentId ,name , age,cityId ) VALUES (1,'Mahdi',39 ,3)".trimIndent())
//Fill teacher table
db.execSQL("INSERT OR IGNORE INTO teacher_table (teacherId, name , grade ) VALUES (1,'Zahra',99 )".trimIndent())
db.execSQL("INSERT OR IGNORE INTO teacher_table (teacherId, name , grade ) VALUES (2,'Shaby',120 )".trimIndent())
//Fill course
db.execSQL("INSERT OR IGNORE INTO course_table (courseId , studentId,teacherId ) VALUES (1,1,1 )".trimIndent())
db.execSQL("INSERT OR IGNORE INTO course_table (courseId , studentId,teacherId ) VALUES (5,1,2 )".trimIndent())
...
我想要一个结果,显示学生通过这样的课程与教师联系:
data class StudentAndTeachers(
val student: Student,
val teachers: List<Teacher>
)
所以我期望看到教师 1、2 与学生 1 的关系。
在 sql 中,我做了这样的连接:
SELECT * FROM student_table LEFT join course_table on course_table.studentId = student_table.studentId LEFT JOIN teacher_table on course_table.teacherId = teacher_table.teacherId group by student_table.studentId
但是结果不会填充教师列表,并且每个学生只包含一名教师,如下所示,但在我的表中我应该看到学生(studentId = 1)
那么我如何才能使连接结果创建我的所有关系的列表?
User1 (listOf(teacher1, teacher2))
I have many-to-many relation on two entities: Student
and Teacher
. They common table is course
. Every student can have a course with a teacher. This is table schemas I'm filling them with initial test values:
//Fill student table
db.execSQL("INSERT OR IGNORE INTO student_table (studentId ,name , age,cityId ) VALUES (1,'Mahdi',39 ,3)".trimIndent())
//Fill teacher table
db.execSQL("INSERT OR IGNORE INTO teacher_table (teacherId, name , grade ) VALUES (1,'Zahra',99 )".trimIndent())
db.execSQL("INSERT OR IGNORE INTO teacher_table (teacherId, name , grade ) VALUES (2,'Shaby',120 )".trimIndent())
//Fill course
db.execSQL("INSERT OR IGNORE INTO course_table (courseId , studentId,teacherId ) VALUES (1,1,1 )".trimIndent())
db.execSQL("INSERT OR IGNORE INTO course_table (courseId , studentId,teacherId ) VALUES (5,1,2 )".trimIndent())
...
I wan to have a result that show students that connected to teachers by courses like this:
data class StudentAndTeachers(
val student: Student,
val teachers: List<Teacher>
)
So I expected to see teacher 1, 2 in relation with student 1.
In sql I did a join like this:
SELECT * FROM student_table LEFT join course_table on course_table.studentId = student_table.studentId LEFT JOIN teacher_table on course_table.teacherId = teacher_table.teacherId group by student_table.studentId
But result wont fill list of teachers and only contain one teacher per student like bellow, but in my table I should see two teachers ( teacher id 1,2 ) for student ( studentId = 1 )
So how I can make result of join create list of my all relation ?
User1 (listOf(teacher1, teacher2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难想象这个模式是什么。但看起来您没有多对多关系,因为您可以通过诸如映射表、参考表、参考表等术语来完成此操作关联表 ....(基本上都是同一件事)
这样的表通常有两列,一列引用/映射其中一个表(例如学生),另一列引用/映射到另一个表,例如课程。
无论如何,这是您似乎想要实现的目标的一个示例。它有一张学生表、一张教师表和一张课程表。课程表包括一个引用/映射教师的列(假设每门课程有一名教师)。最后是映射表,它满足多对多关系,允许一门课程有许多学生,并且一个学生可以参加许多课程。
实体是:-
Teacher
Student
Course
最后映射表实体 StudentCourseMap
支持上述内容并获取课程,与教师和课程中的所有学生一起,有一个 POJO 类 CourseWithTeacherAndStudents
这与您问题中的类类似(但也包括课程详细信息),并且是沿着您想要从数据库检索的内容。
为了利用上面的内容,AllDAO是一个@Dao注释的抽象类(可以是一个接口,但如果你想处理来自你自己的JOINS的数据,抽象类可以有带有body的函数)。
将它们与
活动中如下
:-
运行上述内容(仅一次)时输出到日志的结果是:-
It is hard to imagine what the schema is. But it would appear that you do not have a many to many relationship as you would accomplish this with what is known by terms such as mapping table, reference table, associative table .... (all basically the same thing)
Such a table typically has two columns one that references/maps one of the tables (e.g. student) and the other column references/maps to the other table such as the course.
Anyway here is an example of what you appear to be trying to achieve. It has a table for Students, a table for Teachers and a table for Courses. The course table includes a column that references/maps the teacher (assuming one teacher per course). Lastly there is the mapping table that caters for the many-many relationship that allows a course to have many students and for a student to be in many courses.
The Entities are :-
Teacher
Student
Course
Last the mapping table Entity StudentCourseMap
To support the above and get a Course, with the Teacher and with All the Students in the course there is a POJO class CourseWithTeacherAndStudents
This is similar to the class in your question (but also includes the course details) and is along the lines of what you want to retrieve from the database.
To utilise the above the AllDAO is an @Dao annotated abstract class (can be an interface but if you want to process data from your own JOINS and abstract class can have functions with bodies ).
Putting it all together with a working example
is the following in an Activity
:-
The result output to the log when running the above (just once) is :-