如何创建确定员工年龄分类的函数

How to create a function that determines the age classification of an employee

我正在为 class 做作业,我应该确定此人的年龄是否介于以下类别之一之间:

AGE            CLASSIFICATION
0-14           Children
15-24          Early working age
25-54          Prime working age 
55-64          Mature working age 
65 and above   Elderly 

这是我的脚本目前的样子:

DELIMITER //
CREATE FUNCTION `Employee_age_classification`(`Employees_Last_Name` VARCHAR(50)) RETURNS varchar(50) CHARSET utf8mb4 
    DETERMINISTIC
BEGIN
DECLARE CLASS VARCHAR(50);
DECLARE AGE INT;
DECLARE LASTN VARCHAR(50);
SELECT TIMESTAMPDIFF(YEAR,Bdate,CURDATE())
INTO AGE
FROM EMPLOYEE
WHERE Lname = Employees_Last_Name;
IF AGE>=0 AND AGE<=14 THEN SET CLASS = 'Children';
ELSEIF AGE>=15 AND AGE<=24 THEN SET CLASS = 'Early Working Age';
ELSEIF AGE>=25 AND AGE<=54 THEN SET CLASS = 'Prime Working Age';
ELSEIF AGE>=55 AND AGE<=64 THEN SET CLASS = 'Mature Working Age';
ELSEIF AGE>=65 THEN SET CLASS = 'Elderly';
END IF;
RETURN CLASS;
DELIMITER;

当我尝试 运行 它时显示错误。

Error Code: 1064. You have an error in your SQL syntax. 

我该如何解决这个问题?

您在最后一个 DELIMITER 之前缺少 END 语句 -

DELIMITER //
CREATE FUNCTION `Employee_age_classification`(`Employees_Last_Name` VARCHAR(50))
RETURNS varchar(50) CHARSET utf8mb4 
DETERMINISTIC
BEGIN
     DECLARE CLASS VARCHAR(50);
     DECLARE AGE INT;
     DECLARE LASTN VARCHAR(50);
     SELECT TIMESTAMPDIFF(YEAR,Bdate,CURDATE())
       INTO AGE
       FROM EMPLOYEE
      WHERE Lname = Employees_Last_Name;
     IF AGE>=0 AND AGE<=14 THEN 
        SET CLASS = 'Children';
     ELSEIF AGE>=15 AND AGE<=24 THEN 
        SET CLASS = 'Early Working Age';
     ELSEIF AGE>=25 AND AGE<=54 THEN 
        SET CLASS = 'Prime Working Age';
     ELSEIF AGE>=55 AND AGE<=64 THEN 
        SET CLASS = 'Mature Working Age';
     ELSEIF AGE>=65 THEN 
        SET CLASS = 'Elderly';
     END IF;
     RETURN CLASS;
END//
DELIMITER;