This function has none of DETERMINISTIC, NO SQL.....(you *might* want to use the less safe log_bin_t

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了This function has none of DETERMINISTIC, NO SQL.....(you *might* want to use the less safe log_bin_t相关的知识,希望对你有一定的参考价值。

问题起源:

题:

表: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| salary      | int  |
+-------------+------+
Id是该表的主键列。
该表的每一行都包含有关员工工资的信息。

 

编写一个SQL查询来报告 Employee 表中第 n 高的工资。如果没有第 n 个最高工资,查询应该报告为 null 。

查询结果格式如下所示。

 

示例 1:

输入: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
n = 2
输出: 
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

示例 2:

输入: 
Employee 表:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
+----+--------+
n = 2
输出: 
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| null                   |
+------------------------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/nth-highest-salary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解:

-- 函数
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    set N = N - 1;
  RETURN (
    select ifnull((select distinct Salary from Employee order by Salary desc limit N,1),null)
    as getNthHighestSalary
  );
END
-- 执行
select getNthHighestSalary(2)

问题出现:

问题分析:

这是因为开启了bin-log日志,我们就必须确定函数中以下点

deterministic 不确定的

no sql 没有SQL语句

reads sql data 只读数据

modifies sql data 修改数据

contains sql 包含SQL语句

因为在function中,只有deterministic ,no sql,reads sql data被支持。所以开启bin-log后必须为function指定参数

问题解决:

show variables like ‘log_bin_trust_function_creators’;

set global log_bin_trust_function_creators = 1

show variables like ‘log_bin_trust_function_creators’;

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    set N = N - 1;
  RETURN (
    select ifnull((select distinct Salary from Employee order by Salary desc limit N,1),null)
    as getNthHighestSalary
  );
END

select getNthHighestSalary(2)

补充:

采用以上方法在数据库重启后还会失效,可以使用以下方法长期有效

root@hadoop_zxy ~]# vim /etc/my.cnf

在最后添加

log_bin_trust_function_creators = 1

以上是关于This function has none of DETERMINISTIC, NO SQL.....(you *might* want to use the less safe log_bin_t的主要内容,如果未能解决你的问题,请参考以下文章

MySQL定义函数出现This function has none of DETERMINISTIC 错误

MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法

[Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration a

Mysql 创建函数出现This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA

This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its 错误解决办法

MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法 (转)