Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

GROUP_CONCAT MYSQL group by function

Hello Friends,

GROUP_CONCAT() function is used to concatenate column values into a single string. Note that this function will not count null values. This is very useful group by function if you need records from database with multiple rows in single string with specified separator(e.g. Comma separated). This situation will come in one to may relation of tables. You may get this error while retrieving records using joins. “Operand should contain 1 column(s)”.

It’s better to use this function rather than retrieve all rows from table, manipulate it using PHP function and make a string of records with comma seperated. Look at below simple example.

Table tbl_users have 4 records in column “name”.
– Rakshit
– Pinkesh
– Ankur
– Timir

SELECT name FROM tbl_users;
OutPut:
– Rakshit
– Pinkesh
– Ankur
– Timir

SELECT GROUP_CONCAT(name) FROM tbl_users;
Output:
Rakshit,Pinkesh,Ankur,Timir

Let me know if you find any problem.

Share:

No Comments Yet.

Leave a Reply

Your email address will not be published.