Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sequelize-匹配mySQL数据库中给定实体的所有记录_Mysql_Sequelize.js - Fatal编程技术网

Sequelize-匹配mySQL数据库中给定实体的所有记录

Sequelize-匹配mySQL数据库中给定实体的所有记录,mysql,sequelize.js,Mysql,Sequelize.js,我有三张桌子 表1: students (contains details of all students) - id - full_name - email 记录: | id | full_name | email | |----|-----------|-------------------| | 1 | John | john@example.com | | 2 | Adam | adam@example.com | | 3 | Ja

我有三张桌子

表1:

students (contains details of all students)
- id
- full_name
- email
记录:

| id | full_name | email             |
|----|-----------|-------------------|
| 1  | John      | john@example.com  |
| 2  | Adam      | adam@example.com  |
| 3  | James     | james@example.com |
| 4  | Jane      | jane@example.com  |
| id | title  |
|----|--------|
| 1  | PHP    |
| 2  | Python |
| 3  | .Net   |
| 4  | HTML   |
| id | student_id | course_id |
|----|------------|-----------|
| 1  | 1          | 1         |
| 2  | 1          | 2         |
| 3  | 2          | 3         |
| 4  | 3          | 1         |
表2:

courses (contains all courses)
- id
- title
记录:

| id | full_name | email             |
|----|-----------|-------------------|
| 1  | John      | john@example.com  |
| 2  | Adam      | adam@example.com  |
| 3  | James     | james@example.com |
| 4  | Jane      | jane@example.com  |
| id | title  |
|----|--------|
| 1  | PHP    |
| 2  | Python |
| 3  | .Net   |
| 4  | HTML   |
| id | student_id | course_id |
|----|------------|-----------|
| 1  | 1          | 1         |
| 2  | 1          | 2         |
| 3  | 2          | 3         |
| 4  | 3          | 1         |
表3:

student_courses (this table contains which student has subscribed to what courses)
- student_id
- course_id
记录:

| id | full_name | email             |
|----|-----------|-------------------|
| 1  | John      | john@example.com  |
| 2  | Adam      | adam@example.com  |
| 3  | James     | james@example.com |
| 4  | Jane      | jane@example.com  |
| id | title  |
|----|--------|
| 1  | PHP    |
| 2  | Python |
| 3  | .Net   |
| 4  | HTML   |
| id | student_id | course_id |
|----|------------|-----------|
| 1  | 1          | 1         |
| 2  | 1          | 2         |
| 3  | 2          | 3         |
| 4  | 3          | 1         |
我在这里面临的问题是,我需要得到一份选择了课程ID 1和2的所有学生的列表,在上面的例子中,这两个学生是John

使用sequelize,我尝试了以下两个where子句,但都给出了错误的结果

选项1这给了我空的结果集

where: {
    course_id: {
        [Op.and]: [1,2]
    }
}
选项2这是返回约翰和詹姆斯。它不应该返回James,因为他只订阅了课程id 1

where: {
    course_id: [1, 2]
}
我错过了什么


提前感谢

您可以使用此功能实现N:M关联,更多信息可在此处找到


在非续集术语中,您需要两次在学生课程上进行内部联接,一次为课程id=1,第二次为课程id=2。为什么您在where子句中提到{全名:'john'}?where子句应该包含课程ID,即1和2,它应该返回John的详细信息,因为他是唯一订阅了1和2两门课程的人。您可以根据您的要求进行更新。我已经根据您的条件更新了代码。太好了!,如果我遇到任何问题,我会尝试一下,然后回来。非常感谢你的帮助!更新:尝试了你的解决方案,它给了我空数组结果。