1.创建一个学生成绩登记表,插入一些记录,编写一个统计不及格学生数的存储过程,要求采用循环语句和条件判断语句实现。Sno varchar(30),--学号 Sc varchar(20),--课程号 Score numeric(10,2)—成绩
时间: 2023-05-31 10:05:29 浏览: 136
Excel模板初中学生成绩统计表(模板).zip
CREATE TABLE ScoreRecord (
Sno varchar(30),
Sc varchar(20),
Score numeric(10,2)
);
INSERT INTO ScoreRecord (Sno, Sc, Score) VALUES
('001', 'Math', 85.5),
('002', 'Math', 65.0),
('003', 'Math', 72.3),
('004', 'Math', 54.8),
('005', 'Math', 91.2),
('006', 'Math', 78.6),
('007', 'Math', 67.9);
DELIMITER //
CREATE PROCEDURE CountFailStudents()
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE failCount INT DEFAULT 0;
DECLARE totalRows INT;
SELECT COUNT(*) INTO totalRows FROM ScoreRecord;
WHILE i < totalRows DO
SELECT Score INTO @score FROM ScoreRecord LIMIT i, 1;
IF @score < 60 THEN
SET failCount = failCount + 1;
END IF;
SET i = i + 1;
END WHILE;
SELECT failCount;
END//
DELIMITER ;
CALL CountFailStudents(); -- 结果为2,即不及格学生数为2
阅读全文