24 Apr
Mysql group concatenate select to string
In select query if you want to concatenate all values of a column(all cells) in a single string with a separator. Then you can use a pre defined function in MySql which is called GROUP_CONCAT().
Syntax:Â
SELECT GROUP_CONCAT( column_name SEPARATOR  ',') FROM table_name;
Example:
Id | Conntry_name |
1 | India |
2 | Pakistan |
3 | Shrilanka |
4 | USA |
5 | India |
6 | Nepal |
7 | Bhutan |
8 | China |
SELECT GROUP_CONCAT(country_name   SEPARATOR ',') as all_countries FROM country;
Result:
all_countries |
India,Pakistan,Shrilanka,USA,India,Nepal,Bhutan,China |
Distinct Example:
SELECT GROUP_CONCAT(DISTINCT country_name   SEPARATOR ',') as all_countries FROM country;
Result:
all_countries |
India,Pakistan,Shrilanka,USA,Nepal,Bhutan,China |
If you are not aware about group concat then you can read on below link, hope it will helpful
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
I am a software engineer who specializes in Internet applications. I have worked with a wide variety of technologies and programming languages to open source LAMP environments. I have more than 6 years of object-oriented programming experience and am highly proficient in ActionScript, PHP, MYSQL, JavaScript, Jquery and a multitude of other technologies used in modern web applications.
Follow me
Latest posts by Rajeev Achra (see all)
- Jquery webcam plugin - June 19, 2016
- How To Add and Delete Users on a CentOSServer - June 5, 2016
- How To Set Up vsftpd on CentOS 6 - June 5, 2016