1、查看是否能创建函数

show variables like '%fun%';

 

 如果为OFF,表示不能创建函数

 

2、修改数据库能创建函数

set global log_bin_trust_function_creators = 1;

 

这样就修改为ON了,就能创建函数了

 

3、创建函数(机制和创建存储过程差不多)

delimiter //
create function fun_add(a int,b int)

returns int

begin

return a+b;

end;

//

 

4、调取fun_add函数

select fun_add(2,3);

 

5、删除函数

 

drop function if exists fun_add;

 

 

6、查看函数

 

show create function fun_add;

 

 

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
SET M = N - 1;
RETURN (
      # Write your MySQL query statement below.
     SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M,1),NULL)
  );
END