gpt4 book ai didi

mysql - 从 MySQL 函数返回一个值

转载 作者:行者123 更新时间:2023-11-29 06:55:33 24 4
gpt4 key购买 nike

我写了一个小函数:

CREATE TABLE states
(`id` int, `name` varchar(2))
;

INSERT INTO states
(`id`, `name`)
VALUES
(1, 'md'),
(2, 'tx'),
(3, 'ma')
;

delimiter //

create function states_repeated (s varchar(2))
returns int
begin
insert into sid select count(*) from states where states.name=s ;
return sid ;
end//

delimiter ;

select states_repated('ma') ;

但这会返回

ERROR 1146 (42S02): Table 'test.sid' doesn't exist

如何返回这个值?

最佳答案

尝试这样的事情,

DECLARE _returnValue INT;
SET _returnValue = (select count(*) from states where states.state = s);
return _returnValue;

完整代码

delimiter //

create function states_repeated (s varchar(2))
returns int
begin
DECLARE _returnValue INT;
SET _returnValue = (select count(*) from states where states.name = s);
return _returnValue;
end//

delimiter ;

关于mysql - 从 MySQL 函数返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12944953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com