1

Having following Foo table:

id | name    | group
===|=========|======
1  | "John"  | 1
2  | "Paul"  | 1
3  | "Marry" | 1
4  | "Josh"  | 1
5  | "Mike"  | 2
6  | "Kevin" | 2
7  | "Ramon" | 2
8  | "Kim"   | 2
9  | "Henry" | 3
10 | "Susan" | 3
11 | "Greg"  | 3
12 | "Julie" | 3

I'd like to select it having cycling order on group field. For example, when I would like to select 6 rows, query should return:

id | name    | group
===|=========|======
1  | "John"  | 1
5  | "Mike"  | 2
10 | "Henry" | 3
2  | "Paul"  | 1
6  | "Kevin" | 2
11 | "Susan" | 3

important - there are unlimited groups in the table, so using CASE or any verbose function is pointless.

Is this possible using MySQL? So far I searched for custom order functions. I also thought about to iterate over group ids and use this as sorting, but I'm stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.