.创建存储过程no_jg,实现将选修指定号课程的学生的grade1的不及格率控制在指定范围内,制定了如下加分规则:每人都加5分,看是否满足不及格率在指定范围以下,如果没有,则每人再加5分,再看是否满足,如此反复加分,直到满足条件为止。 执行存储过程no_jg,要求将K001的grade1成绩的不及格率控制在20%以内。
时间: 2023-06-25 08:07:40 浏览: 132
首先,我们需要创建一个存储过程来实现上述需求:
```mysql
DELIMITER $$
CREATE PROCEDURE no_jg (IN course_no VARCHAR(10), IN max_fail_rate FLOAT)
BEGIN
DECLARE current_fail_rate FLOAT DEFAULT 1;
DECLARE add_score INT DEFAULT 5;
WHILE current_fail_rate > max_fail_rate DO
UPDATE student SET grade1 = grade1 + add_score
WHERE sno IN (
SELECT DISTINCT sc.sno FROM sc
WHERE sc.cno = course_no AND sc.grade < 60
);
SELECT COUNT(*) INTO @fail_count FROM sc
WHERE cno = course_no AND grade < 60;
SELECT COUNT(*) INTO @total_count FROM sc
WHERE cno = course_no;
SET current_fail_rate = @fail_count / @total_count;
SET add_score = add_score + 5;
END WHILE;
END $$
DELIMITER ;
```
然后,我们可以调用存储过程no_jg来将K001的grade1成绩的不及格率控制在20%以内:
```mysql
CALL no_jg('K001', 0.20);
```
也就是说,我们要求在不及格率小于等于20%的情况下,不断给K001的学生们加5分,直到满足条件为止。如果不及格率一开始就小于等于20%,则不会进行任何操作。
阅读全文