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部分は 1 を返します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 に答える