我有一张表fanclubs (f_count_m /*count of members*/, id_band /*id of the music band*/)
,我需要一个能够返回最受欢迎乐队的id_band
的函数。
代码:
delimiter //
create function best_of (b varchar(2))
returns varchar(6)
begin
DECLARE b varchar(6);
SET @b =
(select s_and_id.id_band from
(select sum(f_count_m), id_band
from fanclubs
group by id_band
order by f_count_m desc
LIMIT 0,1) as s_and_id);
return b;
end//
delimiter ;
select
部分返回一个id
。但是,如果我尝试使用这样的创建函数:
select @best_of
或
select * from fanclubs where id_band = @best_of
我得到了NULL。
与@b
我哪里错了?
答案 0 :(得分:2)
怎么样
SELECT id_band FROM fanclubs ORDER BY f_count_m DESC LIMIT 1;
我没有测试过这个(我在平板电脑上),但我觉得逻辑很合理。