mySQL-5 najlepszych z każdej kategorii

Chcę mieć możliwość zwrócenia 5 menu na każde menu. Próbowałem tego skryptu kilka, ale nie miałem szczęścia. oto tabele

menus
-------
menuid int()
profileName varchar(35)

menuitems
-----------
itemid int()
name varchar(40)

Oto, co mam teraz. Otrzymuję komunikat o błędzie za pomocą skryptu poniżej. Błąd: zapytanie podrzędne zwraca więcej niż 1 wiersz.

SELECT m.profilename, name
FROM menus m 
WHERE (SELECT name
        from menuitems s
        where m.menuid = s.menuid
        limit 5)
Każda sugestia jest bardzo doceniana.
Author: Joe Stefanelli, 2011-01-19

2 answers

Musisz użyć zmiennych oddziałujących pobocznie do tego

SELECT profilename, name
FROM
(
    SELECT m.profilename, s.name,
        @r:=case when @g=m.profilename then @r+1 else 1 end r,
        @g:=m.profilename
    FROM (select @g:=null,@r:=0) n
    cross join menus m 
    left join menuitems s on m.menuid = s.menuid
) X
WHERE r <= 5
 13
Author: RichardTheKiwi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-18 21:59:05
SELECT TOP 5 m.profilename, s.name
FROM menus m INNER JOIN menuitems s ON m.menuID = s.menuid
 -3
Author: Matt Cook,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-18 21:52:13