Mysql SQL错误:1064您的SQL语法有错误;

Mysql SQL错误:1064您的SQL语法有错误;,mysql,Mysql,我对下一个代码有问题: create function proc1 (id int) returns float begin declare sum float select sum = (note1+note2+note3)/3 from test1.note where note.id=id; return sum; end 我得到了这个错误: #1064 - You have an error in your SQL syntax; check the manua

我对下一个代码有问题:

create function proc1 (id int)
returns float
begin
    declare sum float
    select sum = (note1+note2+note3)/3 from test1.note where note.id=id;
    return sum;
end
我得到了这个错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select sum = (note.note1+note.note2+note.note3)/3 from test1.note where note.id=' at line 5 

我花了很多时间搜索解决方案,但没有找到任何解决方案:(

使用set而不是select或better return而不声明变量:

delimiter //
create function proc1 (id int)
returns float
begin
    return (select (note1+note2+note3)/3 from test1.note where note.id=id);
end
//
delimiter ;
delimiter //
create function proc1 (id int)
returns float
begin
    declare sum float;
    set sum = (select (note1+note2+note3)/3 from test1.note where note.id=id);
    return sum;
end
//
delimiter ;
或使用变量:

delimiter //
create function proc1 (id int)
returns float
begin
    return (select (note1+note2+note3)/3 from test1.note where note.id=id);
end
//
delimiter ;
delimiter //
create function proc1 (id int)
returns float
begin
    declare sum float;
    set sum = (select (note1+note2+note3)/3 from test1.note where note.id=id);
    return sum;
end
//
delimiter ;
您有多个错误:

  • 没有定义分隔符。否则DB认为您的函数定义在第一个
    处停止,这是错误的
  • 您需要使用
    select…into
    。否则会出现“不允许从函数返回结果集”错误
  • 您在声明后忘记了分号

您在同一条语句中有
declare
select
。这一定是打字错误。在“declare sum float;”后面加一个分号是导致否决票的原因。使用“set”或指出不必声明变量?