让我们说学生可以排队参加多个不同的课程。每个课程都有自己的队列与学生。
表格如下: StudentID (int), CourseID (int),接受(bool)和 QueueIndex (INT)。对于每个studentID
,每个QueueIndex
都有不同的courseID
。
如果学生是例如,我如何为所有队列更新每个QueueIndex
删除了他所属的地方?
E.g。学生A在课程A中获得QueueIndex
5,在课程B中获得QueueIndex
3.我希望其他所有学生QueueIndex
以及CourseID
获得更低的 - 值,所以课程A中具有QueueIndex
6的下一位学生将有QueueIndex
5等等。
UPDATE [Register]
SET QueueIndex = QueueIndex -1
WHERE QueueIndex > 0
AND (decrease QueueIndex for every CourseID where that specific StudentID left the queue);
单个查询是否可以实现这一目标?
答案 0 :(得分:1)
如果他们在多个课程中,那么一个学生会多次在那里?如果他们离开了一门课程那么......
UPDATE table SET queue = queue - 1 WHERE queue > queuenumber /*the student that was removed queue number*/ AND course = courseid /*the course id of the course that the student was removed from */
我在手机上很抱歉如果有拼写错误。请告诉我。
答案 1 :(得分:1)
为什么你需要更新queueindex呢?
input id="ratetag_rateslot_attributes_unique_id" name="ratetag[rateslot_attributes][unique_id]"
无论是谁被删除,这都会为你提供队列索引。
答案 2 :(得分:1)
当触发DELETE语句时,您可以使用trigger来执行更新。下面的代码可以帮助您:
CREATE TRIGGER tr_Delete_Update_Register ON dbo.Register
FOR DELETE
AS
BEGIN
UPDATE Register
SET QueueIndex = QueueIndex - 1
WHERE QueueIndex > 0
AND QueueIndex > ( SELECT QueueIndex
FROM deleted
)
END
此触发器仅更新QueueIndex的所有QueueIndex,大于删除的QueueIndex。 否则,如果要更新所有QueueIndex,只需从WHERE子句中注释掉AND条件。