Php Mysql-获取表中不存在的结果依赖于另一个表

Php Mysql-获取表中不存在的结果依赖于另一个表,php,mysql,Php,Mysql,我有两张桌子 Students table: +----------+---------+ |Student Id| Name | +----------+---------+ | 11 | Jack | +----------+---------+ | 30 | Tom | +----------+---------+ | 35 | David | +----------+---------+ | 66 | Mia

我有两张桌子

Students table: +----------+---------+ |Student Id| Name | +----------+---------+ | 11 | Jack | +----------+---------+ | 30 | Tom | +----------+---------+ | 35 | David | +----------+---------+ | 66 | Mia | +----------+---------+ | 90 | Daniel | +----------+---------+ | 100 | Steve | +----------+---------+ Student_course_number table : +--------+------------+--------------+ | ID |Student Id |course number | +--------+------------+--------------+ | 1 | 66 | 102 | +--------+------------+--------------+ | 2 | 66 | 103 | +--------+------------+--------------+ | 3 | 66 | 40 | +--------+------------+--------------+ | 4 | 66 | 41 | +--------+------------+--------------+ | 5 | 30 | 55 | +--------+------------+--------------+ | 6 | 30 | 103 | +--------+------------+--------------+ | 7 | 35 | 40 | +--------+------------+--------------+ | 8 | 35 | 41 | +--------+------------+--------------+ | 9 | 90 | 55 | +--------+------------+--------------+ | 10 | 90 | 100 | +--------+------------+--------------+ | 11 | 11 | 40 | +--------+------------+--------------+ | 12 | 11 | 41 | +--------+------------+--------------+ | 13 | 11 | 55 | +--------+------------+--------------+ I would like to get the following output: Student DID NOT participate in the course number 103 +-----------+----------+ |Student Id | Name | +-----------+----------+ | 11 | Jack | +-----------+----------+ | 35 | David | +-----------+----------+ | 90 | Daniel | +-----------+----------+ | 100 | Steve | +-----------+----------+ 学生表: +----------+---------+ |学生Id |姓名| +----------+---------+ |11 |杰克| +----------+---------+ |30 |汤姆| +----------+---------+ |35 |大卫| +----------+---------+ |66 |米亚| +----------+---------+ |90 |丹尼尔| +----------+---------+ |100 |史蒂夫| +----------+---------+ 学生\课程\编号表: +--------+------------+--------------+ |ID |学生ID |课程号| +--------+------------+--------------+ | 1 | 66 | 102 | +--------+------------+--------------+ | 2 | 66 | 103 | +--------+------------+--------------+ | 3 | 66 | 40 | +--------+------------+--------------+ | 4 | 66 | 41 | +--------+------------+--------------+ | 5 | 30 | 55 | +--------+------------+--------------+ | 6 | 30 | 103 | +--------+------------+--------------+ | 7 | 35 | 40 | +--------+------------+--------------+ | 8 | 35 | 41 | +--------+------------+--------------+ | 9 | 90 | 55 | +--------+------------+--------------+ | 10 | 90 | 100 | +--------+------------+--------------+ | 11 | 11 | 40 | +--------+------------+--------------+ | 12 | 11 | 41 | +--------+------------+--------------+ | 13 | 11 | 55 | +--------+------------+--------------+ 我希望获得以下输出: 学生没有参加103号课程 +-----------+----------+ |学生Id |姓名| +-----------+----------+ |11 |杰克| +-----------+----------+ |35 |大卫| +-----------+----------+ |90 |丹尼尔| +-----------+----------+ |100 |史蒂夫| +-----------+----------+
  • 有些学生没有参加任何课程

您可以使用
不在
中的操作员:

SELECT * 
FROM   students
WHERE  student_id NOT IN (SELECT student_id
                          FROM   student_course_number
                          WHERE  course_number = 103)

您需要使用Left-Outer-Join来获取所有记录,这些记录存在于学生中,但不存在于int课程中

SELECT * FROM Students 
LEFT OUTER JOIN Student_course_number 
ON Students.Student_Id = Student_course_number.Student_Id
WHERE Student_course_number.ID IS null