0

我有一张桌子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

我哪里错了?

4

1 回答 1

2

怎么样

SELECT id_band FROM fanclubs ORDER BY f_count_m DESC LIMIT 1;

我还没有测试过这个(我在我的平板电脑上),但我认为逻辑是合理的。

于 2013-06-04T13:47:19.453 回答