2014-03-12 27 views
0

我的產品在MySQL表列表如下:MySQL的排序由重複模式

id_manufacturer | id_product 
1     1 
2     5 
3     6 
4     10 
1     14 
2     18 
3     25 
4     27 
1     28 
2     29 
3     30 
4     35 
1     40 
2     42 
3     45 
4     55 

我想要的結果是要回的製造商訂購的產品按製造商分組,但只有每3,然後列表再次重複該模式:

id_manufacturer | id_product 
1     1 
1     14 
1     28 
2     5 
2     29 
2     18 
3     6 
3     25 
3     30 
4     27 
4     35 
4     10 
1     154 
1     145 
1     285 

有人可以幫忙嗎?

謝謝!

+0

發佈您的請求代碼 –

回答

0

您可以對本地變量使用嵌套查詢。 試試這個:

select 
    id_manufacturer, 
    id_product 
from (
       select 
        id_manufacturer, 
        id_product, 
        case when (@prev_id <> id_manufacturer) then (@num := 1) else (@num := @num + 1) end as dummy1, 
        (@prev_id := id_manufacturer) as dummy2, 
        @num as rownum 
       from tbl, (select @prev_id := -1, @num := 0) x 
       order by 
        id_manufacturer asc, 
        id_product asc 
) subq 
where 
    rownum <= 3