mysql - 我的第一个 MySQL 函数出现问题。一个接一个地出现错误

标签 mysql function

所以,我正在制作我的第一个 MySQL 函数。

BEGIN
DECLARE ID INT;  
SELECT
    LandID INTO ID
FROM
    Landen
WHERE
    Landnaam = landnaam;
RETURN ID;
END

参数是varchar。 那么,假设我在 Landen 中获得了一 strip 有 Landnaam 'Nederland' 的记录。

我运行我的函数,将“Nederland”作为参数。 但随后它给了我以下错误:

1172 - 结果包含多行

这根本没有意义。因为当我提供数据库中不存在的参数(例如“ASDASD”)时,我会得到相同的错误。 当限制结果时

LIMIT 1

它总是返回 1。

我做错了什么?

最佳答案

drop function if exists myfunc;
delimiter //
create function myfunc(str varchar(50))
returns int
reads sql data
begin
declare id int;  
select landid into id from Landen where landnaam = str limit 1;
return id;
end//
delimiter ;

select myfunc('nederland');

关于mysql - 我的第一个 MySQL 函数出现问题。一个接一个地出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766484/

相关文章:

php - 使用 PHP 和 strotime() 函数查看和更改 mysql 上的数据

function - 在不同区间绘制多个函数 (Mathematica)

php - PostGIS 函数集成在 php 示例中?

mysql - 从表中选择 *,其中所需期间与现有期间不重叠

mysql - MySQL 中的重复或不可预测的结果

mysql - Innodb 崩溃,mysql 直到服务器重新启动后才启动

r - 能够指定 ggplot 美学以返回 ggplot 对象的功能

mysql - 从 PHPMyAdmin 导出不会导入

mysql - 我可以在触发器中创建事件吗?

javascript - 使用纯 JS 创建具有 id 和样式的元素