我想要检索提交表中不存在但存在于Students表中的学生姓名和学生ID,并将其绑定到GridView中。例如,A班有40名学生,但只有38名学生提交了该项目。我希望那些没有提交的学生出现在教师可以查看的GridView中
SELECT *
FROM Students
LEFT JOIN dbo.Submission ON Students.Student_Id = Submission.Student_Id
AND Students.Subject_Id = Submission.Subject_Id
WHERE Students.Subject_Class='" + Session["Subject_Class"].ToString() + "'
AND Students.Subject_Id = '" + Session["Subject_Id"].ToString() + "'
AND Submission.Proj_Sub = '" + Session["Proj_Sub"].ToString() + "'
And Submission.Student_Id IS NULL
当我使用上述语句时,GridView似乎是空的。
答案 0 :(得分:1)
你可能想要这样的东西。
SELECT *
FROM Students
WHERE Students.Subject_Class='" + Session["Subject_Class"].ToString() + "'
AND Students.Subject_Id = '" + Session["Subject_Id"].ToString() + "'
AND NOT EXISTS (SELECT *
FROM Submissions
WHERE Submissions.Student_Id = Students.Student_Id
AND Submissions.Subject_Id = Students.Subject_Id
AND Submissions.Proj_Sub = '" + Session["Proj_Sub"].ToString() + "')
你可以采取多种方法,但这似乎是最干净的。但最重要的是,您应该重点放弃目前易于SQL注入的方法。请参数化您的查询。