Sql 不等于操作员不工作

Sql 不等于操作员不工作,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,SP: 内部联接StudentSubjectMapping SS ON SM.StudentID=SS.StudentID 其中SM.CourseID=@CourseID或@CourseID=0 和SM.ClassID=@ClassID或@ClassID=0 和SM.ClassSectionID=@ClassSectionID或@ClassSectionID=0 和SD.StudentID SS.StudentID 尝试在以下内容中添加括号(): INNER JOIN StudentSubjec

SP:

内部联接StudentSubjectMapping SS ON SM.StudentID=SS.StudentID
其中SM.CourseID=@CourseID或@CourseID=0
和SM.ClassID=@ClassID或@ClassID=0
和SM.ClassSectionID=@ClassSectionID或@ClassSectionID=0
和SD.StudentID SS.StudentID
尝试在以下内容中添加括号
()

INNER JOIN StudentSubjectMapping SS ON SM.StudentID=SS.StudentID  
WHERE SM.CourseID=@CourseID OR @CourseID=0
  AND SM.ClassID=@ClassID OR @ClassID=0
  AND SM.ClassSectionID=@ClassSectionID OR @ClassSectionID=0
  AND SD.StudentID <> SS.StudentID
其中(SM.CourseID=@CourseID或@CourseID=0)
及
(SM.ClassID=@ClassID或@ClassID=0)
及
(SM.ClassSectionID=@ClassSectionID或@ClassSectionID=0)
及
SD.StudentIDSS.StudentID

改用
左连接

WHERE (SM.CourseID=@CourseID OR @CourseID=0)
      AND
      (SM.ClassID=@ClassID OR @ClassID=0)
      AND
      (SM.ClassSectionID=@ClassSectionID OR @ClassSectionID=0)
      AND
      SD.StudentID<>SS.StudentID

您可以使用
notin
选择那些
StudentSubjectMapping
中不存在的
studentid

    LEFT JOIN StudentSubjectMapping SS ON SM.StudentID=SS.StudentID  
    WHERE SM.CourseID=@CourseID OR @CourseID=0
      AND SM.ClassID=@ClassID OR @ClassID=0
      AND SM.ClassSectionID=@ClassSectionID OR @ClassSectionID=0
      AND SS.StudentID IS NULL

谢谢你的支持。答复:

INNER JOIN StudentSubjectMapping SS ON SM.StudentID=SS.StudentID  
WHERE (SM.CourseID=@CourseID OR @CourseID=0)
  AND (SM.ClassID=@ClassID OR @ClassID=0)
  AND (SM.ClassSectionID=@ClassSectionID OR @ClassSectionID=0)
  AND (SD.StudentID NOT IN (SELECT StudentID FROM StudentSubjectMapping))

什么是SD.StudentID….SDOR在和之前..当使用OR时,您必须非常小心如何使用它,否则结果将是意外的。StudentID在另一个表中。@Jithin,使用LEFT JOIN,请在answer@Jithin什么不起作用?提供示例数据和所需结果。在搜索时,单击“我需要显示不在SS.StudentID中的StudentID”。@Jithin,使用
左连接
,请查看下面答案右侧的示例。不需要使用内部联接。感谢您的支持。是的,请记录:查看SQL运算符优先级;至少和(或;)
WHERE
    (SM.CourseID=@CourseID OR @CourseID=0)
    AND
    (SM.ClassID=@ClassID OR @ClassID=0)
    AND
    (SM.ClassSectionID=@ClassSectionID OR @ClassSectionID=0)
    AND
    SM.StudentID NOT IN (SELECT StudentID FROM StudentSubjectMapping)