Mysql 是否有另一种编写sql查询的方法

Mysql 是否有另一种编写sql查询的方法,mysql,sql,Mysql,Sql,我有三张桌子-班级桌子,学生桌子,学生桌子 在这些表格的帮助下,我需要写一个查询,告诉我们没有参加任何课程的学生的情况 我已经编写了一个有效的查询,但我想知道是否有一种替代方法和更简单的方法来编写查询以获得所需的结果 SELECT s.studentName, c.className FROM student s LEFT JOIN studentClass sc ON sc.studentId = s.studentId LEFT JOIN classes c ON sc.class

我有三张桌子-班级桌子,学生桌子,学生桌子

在这些表格的帮助下,我需要写一个查询,告诉我们没有参加任何课程的学生的情况

我已经编写了一个有效的查询,但我想知道是否有一种替代方法和更简单的方法来编写查询以获得所需的结果

SELECT s.studentName, c.className 
FROM student s 
LEFT JOIN studentClass sc 
ON sc.studentId = s.studentId 
LEFT JOIN classes c 
ON sc.classId = c.classId 
WHERE c.className IS NULL

只需检查学生id是否存在于
studentclass

不在:

select * 
from student 
where studentid not in (select studentid from studentclass)
或不存在:

select s.* 
from student s
where not exists (
  select 1 from studentclass
  where studentid = s.studentid
)

只需检查学生id是否存在于
studentclass

不在:

select * 
from student 
where studentid not in (select studentid from studentclass)
或不存在:

select s.* 
from student s
where not exists (
  select 1 from studentclass
  where studentid = s.studentid
)

您的
where
应该检查
连接中使用的键是否为
null
,在本例中为
c.classId
,因为这证明不存在匹配项。
className
中的
null
或任何其他字段可能仅仅意味着键上存在匹配项,但无论出于何种原因,该特定字段为
null
;这是模棱两可的,不是决定性的。你不需要类名table@underscore_d你能帮我解释一下吗code@JamesJenkins你能帮我解释一下吗code@user3732711不。我说在你的
where
子句中用
classId
替换
className
,这一点很简单,在我看来并不值得说明。您的
where
应该检查
连接中使用的键是否为
null
,在本例中是
c.classId
,因为这证明不存在匹配。
className
中的
null
或任何其他字段可能仅仅意味着键上存在匹配项,但无论出于何种原因,该特定字段为
null
;这是模棱两可的,不是决定性的。你不需要类名table@underscore_d你能帮我解释一下吗code@JamesJenkins你能帮我解释一下吗code@user3732711不。我说在你的
where
子句中用
classId
替换
className
,这很简单,在我看来不值得说明。你会使用
存在
谓词来检查存在吗?我也添加了一个不存在的解决方案。你不会使用
存在
谓词来检查存在吗?我也添加了一个不存在的解决方案。