Use of Group_Concat in MySql

vishalneeraj-24503
Posted by vishalneeraj-24503 under Others category on | Points: 40 | Views : 1599
Sometimes we need to have Comma-Separated values from table.
In MySql,it' very very easy. With Group_Concat in-built MySql function,we can achieve such things.

For ex:-
Suppose,i have employee_master table in which employee_id is an auto-generated column.
I want to have comma-separated employee_id,then i will do like:-

Select Group_Concat(employee_id) as 'Employee Id' from Employee_Master where Status = 'AA';


Output will be:-
1,2,3,4,5,6,7,8,12,15

Also we can use order by clause inside Group_Concat function:-

Select Group_Concat(employee_id order by employee_id) as 'Employee Id' from Employee_Master where Status = 'AA';


Group_Concat function adds Comma(,) separator as default but we can take our own separator as anything with the help of Separator inside Group_Concat as shown

Select Group_Concat(employee_id order by employee_id SEPARATOR ';') from Employee_Master where Status = 'AA';


Output will be:-
1;2;3;4;5;6;7;8;12;15

So we can have own choice with Group_Concat function in MySql DB.

Comments or Responses

Login to post response