The use of the GROUP_CONCAT

  • 2020-05-06 11:47:24
  • OfStack

GROUP_CONCAT

Today's work needs to use group by, etc., check the manual, I did not expect to find GROUP_CONCAT this good thing, suddenly saved me a lot of work, this function was added in MySQL 4.1. The str_concatenation () function returns a string result that is a combination of value concatenations in a group.
Post a simple example:
mysql > SELECT * FROM `ta`;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 1 | b |
| 1 | c |
| 1 | d |
| 2 | a |
| 2 | b |
| 2 | c |
| 3 | d |
+----+------+
8 rows in set (0.00 sec)

mysql > SELECT `id`,
- > GROUP_CONCAT(`name`)
- > FROM `ta`
- > GROUP BY `id`;
+----+----------------------+
| id | GROUP_CONCAT(`name`) |
+----+----------------------+
| 1 | a c b d |
| 2 | a c b |
| 3 | d |
+----+----------------------+
3 rows in set (0.03 sec)

From: http: / / bbs phpso. com/journal php? Es89en do = showentry & e = 76


Related articles: