坚持在MySQL中创建一个简单的过程[IF语句]

时间:2016-05-29 03:06:14

标签: mysql sql stored-procedures procedure

我想使用IF语句在MySql中创建一个过程,当我将学生的id作为IN参数时,我可以显示所有平均值大于70的学生的姓名。

是否可以一次提供许多学生ID并使用CALL和SELECT访问更多结果?

学生表:

  

ID,姓名,专业,平均,状态
   '33','Vera Violet',NULL,'92',   'UNCL'
   '94','Pat Pink','POLS','93','UNCL'
    '101','Bob Blue','ECON','73','UNCL'
  '102','怀特怀特','GEOG','64','UNGR'   '302','Yolanda Yellow','HIST','64','UNGR'
    '403','布莱恩布莱克',   'ECON','87','GRAD'
   '576','Ruby Rose','ECON','77','UNCL'
  '992','Greg Green','COMM','86','GRAD'

以下是我的尝试:(但没有奏效)

DELIMITER $$
CREATE PROCEDURE sort_Students_By_Avg( IN student_Id INT(11), OUT student_Name VARCHAR(20))
BEGIN 
DECLARE student_Avg INT(11); 
SELECT avg INTO student_Avg
FROM student
WHERE id=student_Id; 

IF (student_Avg > 70)
THEN SET student_Name = student.name;
END IF;
END $$  

1 个答案:

答案 0 :(得分:1)

这似乎并不特别有用,但程序看起来像这样:

CREATE PROCEDURE sort_Students_By_Avg (
    IN in_student_Id INT(11),
    OUT out_student_Name VARCHAR(20)
)
BEGIN
    SELECT s.name INTO out_student_name
    FROM student s
    WHERE s.id = in_student_Id AND s.avg > 70; 
END;