How to group rows in mysql to be as chunks with a specific size


Problem :

How to group rows in mysql to be as chunks with a specific size ?

I have a table called users

Which has these fields id name

I want to make a query which group users into chucks with the size of 3 names per row.

Sample table:

1 name1
2 name2
3 name4
4 name5
5 name5
6 name6
7 name7

result should be

name1,name2,name3
name4,name5,name6
name7

Usign GROUP_CONCAT of course to do that



Solution :

http://sqlfiddle.com/#!9/a6b42/5

SELECT GROUP_CONCAT(name), 
  (IF(@i = 3 OR @i IS NULL, @i:=1, @i:[email protected]+1 ))as idx,
  (IF(@gr_idx IS NULL, @gr_idx:=1, IF(@i = 1, @gr_idx:[email protected]_idx+1,@gr_idx) )) as gr_idx
FROM users
GROUP BY gr_idx

Mysql Tutorials

Mysql Howto..

How to re-try MySQL connection if the database is temporarily unavailable

how to search for multiple full and partial values in MySQL?

How to get a particular column's values alone?

How to avoid creation of foreign keys on Django Models?

How to use Enterprise library and mysql database with c# 4.0?

How to set predefined value for a field in mysql

How to store an array values in MySQL table?

How to do a many to many subquery in MySql

How to capture a timestamp in MySql programmatically?

How to improve MySql query that trying to find distinct values from two tables?

MySQL InnoDB: how to completely flush all remaining transactional changes

How do I format this time/date in PHP?

How do I return a field in MySQL from a sub-query linked to the main query results?

how do foreign key works in MYSQL?

Nested query is so slow in MySQL. How to optimize it?

How to select the most popular rows from today

How to perform a MySQL insert via jQuery/Ajax within the Zend Framework

How to request MYSQL elements instead of using Array in PHP

MySQL How to obtain the MIN and MAX of the top 100 values in a table

how to use mysql command in ssh? [closed]

How to pass login parameters to mysql

How to get mysql queries if contition is matched and yet have a fallback

how to insert data from jtable to mysql database

Printing how much days left to a subscription?

How to select a dynamic number of rows with mysql

Perl & MySQL - How to get top 100 most frequently used field (containing a number) from column in table?

How to duplicate a MySQL database on the same server

how to convert an array into multidimensional

How to fix java.sql.SQLException: Server is running in --secure-auth mode, but 'user'@'host' has a password in the old format; (…)?

how to save a javascript rendered countdown timer to mysql using PHP?