mysql - 我的 mysql 函数有一个错误

标签 mysql function date

这给出了错误 1064,borrower 是一个表,dataofissue 是发行日期,rollno 是borrower 表中的roll no:

create function get_info(roll in integer,name in varchar(20))
    -> return boolean
    -> begin
    -> set @date1=curdate()
    -> set @date2:=select dataofissue from borrower where rollin=roll and nameofbook=name
    -> select DATEDIFF(@date1,@date2) into @date3 from borrower
    -> select @date3
    -> if(@date3>15)
    -> return (false)
    -> else
    -> return (true)
    -> end;

最佳答案

为了使用 SELECT 查询作为值,您必须将其放在括号中:

set @date2:=(select dataofissue from borrower where rollin=roll and nameofbook=name)

或者您可以使用INTO来分配变量:

select dataofissue INTO @date2 from borrower where rollin=roll and nameofbook=name

此外,函数中的每个语句后面都需要 ; 。为了防止立即结束函数定义,您必须在定义函数之前使用 DELIMITER 指令,并使用该分隔符来结束函数。

IF 需要 THENEND IF

您不能为函数参数指定 INOUTINOUT,这仅适用于过程。

DELIMITER $
create function get_info(roll integer, name varchar(20)) return boolean
begin
    set @date1=curdate();
    set @date2:=(select dataofissue from borrower where rollin=roll and nameofbook=name);
    select DATEDIFF(@date1,@date2) into @date3;
    if (@date3>15)
    then return (false);
    else return (true);
    end if;
end$

关于mysql - 我的 mysql 函数有一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45878666/

相关文章:

MySQL 子查询的奇怪行为

ruby - 无方法错误 : undefined method `zone' for Time:Class

function - 使用 VBA 或 Excel 测试文件是否存在(不带 "DIR")

Javascript 函数返回一个字符串而不是用户输入

具有类似对象属性的 JavaScript 函数

php - 将 jquery datepicker 创建的日期转换为标准的 mysql dateformat

jquery - 自定义输入类型日期

javascript - 从动态创建的选择表单中检索选项

MySQL从左连接表插入一列的所有行?

MySQL 左连接计数不正确