#前提准备
CREATE TABLE employees
AS
SELECT * FROM atguigudb.`employees`;
CREATE TABLE
departments AS
SELECT * FROM atguigudb.`departments`;
#1. 创建函数get_count(),返回公司的员工个数 #有参有返回
SET GLOBAL log_bin_trust_function_creators = 1;
DELIMITER $
CREATE FUNCTION get_count()
RETURNS INT
BEGIN
RETURN (SELECT COUNT(*) FROM employees);
END $
DELIMITER ;
注意:
自己在创建存储函数中报错“you might want to use the less safe log_bin_trust_function_creators variable”,然后查看笔记发现解决此问题的一种方式就是执行SET GLOBAL log_bin_trust_function_creators = 1;SQL语句
#2. 创建函数ename_salary(),根据员工id,返回它的工资
#查询表结构来看要返回数据的类型
DESC employees;
DELIMITER $
CREATE FUNCTION ename_salary(id INT)
RETURNS DOUBLE(8,2)
BEGIN
RETURN (SELECT salary FROM employees WHERE employee_id = id);
END $
DELIMITER ;
#查询结果
SELECT ename_salary(100);