sql - MySQL - group rows in 4's -


i have product table this:

product name , affiliate id , productcode a, 1, 1 b, 1, 2 c, 1, 3 d, 1, 5 e, 1, 7 f, 2, 4 g, 2, 6 

i want return first 4 products each affiliate id. 'productcode' column signifies order in products added, can use column sort results. don't know how return first 4 results each affiliate id? if use 'group' function returns 1 row of each affiliate id.

look example in link:

within-group quotas (top n per group)

it need.

 select affiliateid, productcode   (      select        affiliateid, productcode,        if( @prev <> id  @rownum := 1, @rownum := @rownum+1 ) rank,        @prev := id      table   join (select @rownum := null, @prev := 0) r   order affiliateid, productcode   ) tmp   tmp.rank <= 4  order affiliateid, productcode;  

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -