How to use MySQL group_concat function

Source: Internet
Author: User

Group_concat () calculates which rows belong to the same group and displays the columns that belong to the same group. Which columns to return, by letter

The number parameter (that is, the field name) is determined. Groupings must have a standard, grouped by the columns specified by group BY.

The Group_concat function should have executed the group by statement internally, which is my guess.

1. Test statement:

The code is as follows Copy Code
SELECT Group_concat (town) from ' players ' group by town

The result is to look up town to find out which values are the same, and if they are equal, list them all, listed as comma-separated, as follows:

Group_concat (town)

BEIJING, Beijing
Changsha


A simple example:

  code is as follows copy code

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 ')
-> FRO M ' 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.