27/01/2017 - MYSQL
Assume that a table has many records and you just want to concatenate values of a particular column to show as one record. Check example below to see how it is done.
In this example 1 "manager" can have 0 or more "category" so it is 1-n relationship.
mysql> SELECT * FROM manager;
+----+----------------+
| id | name |
+----+----------------+
| 1 | Robert De Niro |
| 2 | Al Pacino |
| 3 | Andy Garcia |
+----+----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM category;
+----+------+------------+
| id | code | manager_id |
+----+------+------------+
| 1 | A | 1 |
| 2 | B | 1 |
| 3 | A | 2 |
+----+------+------------+
3 rows in set (0.00 sec)
mysql> SELECT
-> `manager`.`name` AS Manager,
-> `category`.`code` AS Category
-> FROM `manager`
-> LEFT JOIN `category` ON `manager`.`id` = `category`.`manager_id`;
+----------------+----------+
| Manager | Category |
+----------------+----------+
| Robert De Niro | A |
| Robert De Niro | B |
| Al Pacino | A |
| Andy Garcia | NULL |
+----------------+----------+
4 rows in set (0.00 sec)
mysql> SELECT
-> `manager`.`name` AS Manager,
-> GROUP_CONCAT(`category`.`code` SEPARATOR ', ') AS Category
-> FROM `manager`
-> LEFT JOIN `category` ON `manager`.`id` = `category`.`manager_id`
-> GROUP BY `manager`.`name`;
+----------------+----------+
| Manager | Category |
+----------------+----------+
| Al Pacino | A |
| Andy Garcia | NULL |
| Robert De Niro | A, B |
+----------------+----------+
3 rows in set (0.00 sec)