仅为缺少 SQL 成绩的学生插入成绩
Insert grades only for student missing those grades with SQL
我是 SQL 的新手,遇到这个问题:我必须检查是否每个学生都有所有成绩 (1-10)。如果学生缺少成绩,我必须将其插入。但是任何学生都不应该拥有相同年级的副本 - 他们应该只拥有一次。
我所做的就是显示所有学生的成绩:
SELECT CONCAT(studentdata.FirstName,studentdata.LastName) AS 'Name', grade.Grade
FROM studentdata
CROSS JOIN grade
WHERE studentdata.Id = grade.IdStudent
ORDER BY Name
如果你的 table gradelevels
只有一列 (Grade
) 可能的成绩值 (1, 2, 3, ..., 10) 你可以做它像这样:
INSERT INTO grade
SELECT
studentdata.Id AS IdStudent,
gradelevels.Grade AS Grade
FROM studentdata -- Get all the students,
CROSS JOIN gradelevels -- one time for each possible grade.
LEFT JOIN grade ON
-- Now we will know what (student, grade) combinations have an entry in grade.
studentdata.Id = grade.IdStudent AND gradelevels.Grade = grade.Grade
WHERE grade.IdStudent IS NULL -- Only take the rows where there isnt already a grade.
如果您不希望有特定的 table 等级,您可以将 CROSS JOIN gradelevels
替换为:
CROSS JOIN (SELECT Grade FROM grade GROUP BY Grade) gradelevels
SELECT
将为您提供所有已使用的成绩,因此如果有一些成绩未被任何学生使用,则不会插入。
我建议您在 table Grade
中的 IdStudent
和 Grade
上有一个唯一索引(或将它们作为主键),以确保没有学生有两次相同的成绩。
您可以使用此查询查看所有有或没有成绩的学生:
Select CONCAT(sd.FirstName,sd.LastName) AS 'Name', grade.Grade
from studentdata sd
left outer join grade
on sd.Id = grade.IdStudent
如果您只能看到没有成绩的学生:
Select CONCAT(sd.FirstName,sd.LastName) AS 'Name', grade.Grade
from studentdata sd
left outer join grade
on sd.Id = grade.IdStudent
WHERE ISNULL(grade.Grade, -1000) = -1000
最后插入默认成绩“5”:
insert into grade (id, grade)
Select CONCAT(sd.FirstName,sd.LastName) AS 'Name', isnull(grade.Grade, 5) as Grade
from studentdata sd
left outer join grade
on sd.Id = grade.IdStudent
WHERE ISNULL(grade.Grade, -1000) = -1000
因为你的成绩 table 似乎包含所有作业的所有成绩,你想在 where 语句中添加一个 "andgrade.assigmentid =
我是 SQL 的新手,遇到这个问题:我必须检查是否每个学生都有所有成绩 (1-10)。如果学生缺少成绩,我必须将其插入。但是任何学生都不应该拥有相同年级的副本 - 他们应该只拥有一次。
我所做的就是显示所有学生的成绩:
SELECT CONCAT(studentdata.FirstName,studentdata.LastName) AS 'Name', grade.Grade
FROM studentdata
CROSS JOIN grade
WHERE studentdata.Id = grade.IdStudent
ORDER BY Name
如果你的 table gradelevels
只有一列 (Grade
) 可能的成绩值 (1, 2, 3, ..., 10) 你可以做它像这样:
INSERT INTO grade
SELECT
studentdata.Id AS IdStudent,
gradelevels.Grade AS Grade
FROM studentdata -- Get all the students,
CROSS JOIN gradelevels -- one time for each possible grade.
LEFT JOIN grade ON
-- Now we will know what (student, grade) combinations have an entry in grade.
studentdata.Id = grade.IdStudent AND gradelevels.Grade = grade.Grade
WHERE grade.IdStudent IS NULL -- Only take the rows where there isnt already a grade.
如果您不希望有特定的 table 等级,您可以将 CROSS JOIN gradelevels
替换为:
CROSS JOIN (SELECT Grade FROM grade GROUP BY Grade) gradelevels
SELECT
将为您提供所有已使用的成绩,因此如果有一些成绩未被任何学生使用,则不会插入。
我建议您在 table Grade
中的 IdStudent
和 Grade
上有一个唯一索引(或将它们作为主键),以确保没有学生有两次相同的成绩。
您可以使用此查询查看所有有或没有成绩的学生:
Select CONCAT(sd.FirstName,sd.LastName) AS 'Name', grade.Grade
from studentdata sd
left outer join grade
on sd.Id = grade.IdStudent
如果您只能看到没有成绩的学生:
Select CONCAT(sd.FirstName,sd.LastName) AS 'Name', grade.Grade
from studentdata sd
left outer join grade
on sd.Id = grade.IdStudent
WHERE ISNULL(grade.Grade, -1000) = -1000
最后插入默认成绩“5”:
insert into grade (id, grade)
Select CONCAT(sd.FirstName,sd.LastName) AS 'Name', isnull(grade.Grade, 5) as Grade
from studentdata sd
left outer join grade
on sd.Id = grade.IdStudent
WHERE ISNULL(grade.Grade, -1000) = -1000
因为你的成绩 table 似乎包含所有作业的所有成绩,你想在 where 语句中添加一个 "andgrade.assigmentid =