Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 896
user2278120 Avatar asked Oct 26 '25 16:10

user2278120


1 Answers

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)
like image 198
Gordon Linoff Avatar answered Oct 29 '25 05:10

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!