php - MySQL sorting by repetitive pattern -
i have list of products in mysql table this:
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 my desired outcome list of products ordered manufacturer grouped manufacturer 3 of each , repeat pattern again:
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 can please?
thank you!
you can use nested queries local variables. try this:
select id_manufacturer, id_product ( select id_manufacturer, id_product, case when (@prev_id <> id_manufacturer) (@num := 1) else (@num := @num + 1) end dummy1, (@prev_id := id_manufacturer) dummy2, @num rownum tbl, (select @prev_id := -1, @num := 0) x order id_manufacturer asc, id_product asc ) subq rownum <= 3
Comments
Post a Comment