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
,这很简单,在我看来不值得说明。你会使用存在
谓词来检查存在吗?我也添加了一个不存在的解决方案。你不会使用存在
谓词来检查存在吗?我也添加了一个不存在的解决方案。