T-SQL查询返回没有连接用户的行

时间:2017-10-06 06:07:19

标签: sql sql-server tsql select

使用T-SQL返回没有学生的课程的CourseName。

StudentCourses:

    StudentCourseID
    StudentID
    CourseID



Courses:

    CourseID
    CourseName

我必须找到CourseName中没有学生的内容。 也许我的大脑是炒的,因为我不知道从哪里开始。 我需要一个Left Join吗?

2 个答案:

答案 0 :(得分:2)

使用not exists运算符可能是最简单的方法:

SELECT coursename
FROM   course c
WHERE  NOT EXISTS (SELECT *
                   FROM   studentcourses s
                   WHERE  s.courseid = c.courseid)

答案 1 :(得分:1)

您可以使用WHERE执行此操作 - 它将返回所有课程和相应的学生。使用SELECT coursename FROM course c LEFT JOIN studentcourses sc ON c.courseid = sc.courseid WHERE sc.courseid IS NULL; 条款,我们只过滤了没有学生的课程:

def string_avg_update(L):
    '''(list of str) -> NoneType
    Given a list of strings where each string has the format:
    'name, grade, grade, grade, ...' update  the given 
    list of strs to be a list of floats where each item 
    is the average of the corresponding numbers in the 
    string. Note this function does NOT RETURN the list.
    >>> L = ['Anna, 50, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
    >>> string_avg_update(L)
    >>> L 
    [74.0, 65.0, 98.0]
    '''

    y = L
    b = string_avg(y)
    k = []
    for item in b:
        k.append(item[1])

    L = k