常用SQL语句收集

筛选选修全部课程的学生

SELECT student.name FROM student WHERE NOT EXISTS
(
    SELECT * FROM course WHERE NOT EXISTS 
        (
            SELECT * FROM student_course WHERE student_course.stuid=student.id
                                                AND course.id=student_course.courseid
        
        )
)

筛选至少选秀了学生A选修的所有课程的学生

SELECT * FROM student,
(SELECT DISTINCT stuid FROM student_course sca 
WHERE NOT EXISTS
(
    SELECT * FROM student_course scb WHERE (scb.stuid=2 AND
                                            NOT EXISTS (SELECT * FROM student_course scc
                                            WHERE scc.stuid=sca.stuid AND scc.courseid=scb.courseid)
                                            )
)) AS temp WHERE temp.stuid=student.id

优化:筛选至少选秀了学生A选修的所有课程的学生

SELECT [name] AS StudentName  FROM student WHERE id in
(SELECT DISTINCT stuid FROM student_course sca 
WHERE NOT EXISTS
(
    SELECT * FROM student_course scb WHERE (scb.stuid=2 AND
                                            NOT EXISTS (SELECT * FROM student_course scc
                                            WHERE scc.stuid=sca.stuid AND scc.courseid=scb.courseid)
                                            )
))
posted @ 2009-09-17 20:00  Kevin Dai  阅读(253)  评论(0编辑  收藏  举报