函数语法:
必须有一个返回值,并且只能是一个返回值

create function(参数名 参数类型) returns 返回类型
begin
    函数体
    return 值;
end 结束符
调用:
select 函数名(参数列表) 

#案例:返回公司的员工个数,无参返回

DELIMITER $
CREATE FUNCTION mm() RETURNS INT
BEGIN
    DECLARE c INT DEFAULT 0;
    SELECT COUNT(*) INTO c
    FROM employees;

    RETURN c;
END $

SELECT mm()$

#有参返回
#案例:根据员工名,返回工资

CREATE FUNCTION mm2(ename VARCHAR(20)) RETURNS DOUBLE
BEGIN
    DECLARE esalary DOUBLE DEFAULT 0;
    SELECT salary INTO esalary
    FROM employees e
    WHERE  e.last_name=ename;
    RETURN esalary;

END $

SELECT mm2('kochhar')$

#案例:根据部门名返回该部门的平均工资

CREATE FUNCTION mm3(dname VARCHAR(20)) RETURNS DOUBLE
BEGIN
    DECLARE asalary DOUBLE DEFAULT  0;
    SELECT AVG(salary) INTO asalary
    FROM employees e
    INNER JOIN departments d
    ON e.department_id=d.department_id
    WHERE d.department_name=dname;
    RETURN asalary;

END $

SELECT mm3('IT')$