Det, du prøver at gøre, er at vælge et antal poster pr. gruppe. Dette kan gøres ved at bruge variabler i din forespørgsel, der ligner denne:
select *
from
(
SELECT sid,
state,
votes,
@prev := @curr,
@curr := state,
@rank := IF(@prev = @curr, @rank+1, 1) AS rank
FROM
(
select t1.sid, state, votes
FROM table1 t1
INNER JOIN table2 t2
ON t1.sid=t2.sid
) src, (SELECT @curr := null, @prev := null, @rank := 1) r
ORDER BY state, votes desc
) src
where rank <= 2
order by state, votes;
Der er andre måder at gøre dette på, som er beskrevet her:
Brug af LIMIT inden for GROUP BY for at få N resultater pr. gruppe?