Search code examples
mysqlsqlgroup-bygroup-concat

MySql GROUP_CONCAT with GROUP BY every set of nth rows


SQL fiddle: http://sqlfiddle.com/#!9/e7f72/2

Assume a table called testt has 10 records in it (id is not null auto increment). If I were to do

SELECT GROUP_CONCAT(id) FROM testt

I would expect the results to look like

1,2,3,4,5,6,7,8,9,10

How could I get the results to look like this:

1,2

3,4

5,6

7,8

9,10


Solution

  • You would need to group by a function of the id. Something like this:

    select group_concat(id order by id)
    from testt
    group by floor((id - 1) / 2)