mysql - Limiting groups with a query -


please take @ following sql fiddle.

http://sqlfiddle.com/#!2/f7939/3

you see query of:

select sub.entry_id, sum(jud.stage), sub.category_id, sub.member_group exp_judging jud left join exp_submissions sub on jud.rel_id = sub.id jud.rel_id = jud.rel_id group jud.rel_id order 3 asc limit 10 

this adding rows have same rel_id, , outputting them.

i need output top x number of each category, lets 3 each category.

so example, results sql fiddle be:

category 1: (entry_id in order of score) 10, 11, 16

category 2: 13, 12, 14

how can achieve this?

while i'm sure there has cleaner solution, introduce row number using user defined variables achieve this:

select * (   select *,     @rn:=if(@prevcat=category_id, @rn + 1,1) rn,     @prevcat:=category_id   (     select sub.entry_id,        sum(jud.stage),        sub.member_group,       sub.category_id     exp_judging jud       left join exp_submissions sub on jud.rel_id = sub.id     group jud.rel_id     order sub.category_id, sum(jud.stage) desc, sub.entry_id   ) t     join (select @rn:=0, @prevcat:=0) t2 ) t rn <= 3 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -