我有一个表student_course如下: -
student course
1 a
2 a
1 b
4 a
5 d
2 d
5 a
我需要找到所有行,其中course = a,而课程不在b中。结果应该是: -
student course
2 a
4 a
5 a
答案 0 :(得分:0)
SELECT *
FROM student_course
WHERE course = 'a'
AND student NOT IN (SELECT student
FROM student_course
WHERE course = 'b');
答案 1 :(得分:0)
SELECT * FROM student_course
WHERE course = 'a' AND student NOT IN
(
SELECT student FROM student_course a
WHERE course = 'b'
)
答案 2 :(得分:0)
select student
from student_course
group by student
having sum(case when course = 'a' then 1 else 0 end) > 0
and sum(case when course = 'b' then 1 else 0 end) = 0
答案 3 :(得分:0)
尝试此查询:
SELECT ca.Student, ca.Course
FROM student_course ca
WHERE ca.course = 'a'
AND NOT EXISTS (SELECT 1 FROM student_course cb
WHERE ca.Student = cb.Student AND cb.course = 'b' )
答案 4 :(得分:0)
使用LEFT JOIN
。
SELECT T1.*
FROM student_course T1
LEFT JOIN student_course T2 ON T1.student = T2.student AND T2.course = 'B'
WHERE T1.course = 'A' AND T2.student IS NULL
答案 5 :(得分:0)
select * from student_course
where course = 'a' and student not in (select student from student_course where course = 'b')