Select rows not in another table, SQL Server query

后端 未结 2 1900
半阙折子戏
半阙折子戏 2020-12-16 11:04

Subject table

CREATE TABLE [dbo].[BS_Subject](
     [SubjectID] [bigint] IDENTITY(1,1) NOT NULL,
     [DepartmentID] [bigint] NOT NULL,
     [SubjectName] [v         


        
相关标签:
2条回答
  • 2020-12-16 11:19
    SELECT SubjectID, DepartmentID, SubjectName, SubjectDescription, SubjectShortCode
    FROM BS_Subject 
    WHERE NOT EXISTS 
    (SELECT SubjectToClassID FROM BS_SubjectToClass WHERE 
    BS_Subject.SubjectID = BS_SubjectToClass.SubjectID
    AND BS_SubjectToClass.ClassID =2)
    
    0 讨论(0)
  • 2020-12-16 11:20

    You need to use the NOT IN operator - not the <> (that's VB or something....)

    SELECT 
       DISTINCT(BS_Subject.SubjectID) DepartmentID, 
       SubjectName, SubjectDescription, SubjectShortCode
    FROM dbo.BS_Subject
    WHERE 
        BS_Subject.SubjectID NOT IN
           (SELECT DISTINCT(BS_Subject.SubjectID)
            FROM dbo.BS_Subject, dbo.BS_SubjectToClass
            WHERE BS_Subject.SubjectID = BS_SubjectToClass.SubjectID
            AND BS_SubjectToClass.ClassID = 2)
    
    0 讨论(0)
提交回复
热议问题