Deep understanding of Group_concat function in Mysql _mysql

Source: Internet
Author: User
This paper introduces the use of Group_concat functions in MySQL, such as select Group_concat (name).
Group_concat function in MySQL
The complete syntax is as follows:
Group_concat ([DISTINCT] The field to connect [order by Asc/desc sort field] [Separator ' delimiter]]
Basic Query
Mysql> select * from AA;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
| 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in Set (0.00 sec)
Grouped by ID, print the value of the Name field on one line, separated by commas (default)
Mysql> Select Id,group_concat (name) from AA Group by ID;
+------+--------------------+
| id| Group_concat (name) |
+------+--------------------+
|1 | 10,20,20|
| 20 |
|3 | 200,500|
+------+--------------------+
3 Rows in Set (0.00 sec)
Grouped by ID, print the value of the Name field on one line, separated by semicolons
Mysql> Select ID,GROUP_CONCAT (name separator '; ') from the AA group by ID;
+------+----------------------------------+
| id| Group_concat (name separator '; ') |
+------+----------------------------------+
|1 | 10;20;20 |
| 20|
|3 | 200;500 |
+------+----------------------------------+
3 Rows in Set (0.00 sec)
Grouped by ID, the value of the Name field to be redundant is printed on one line,
Comma separated
Mysql> Select Id,group_concat (distinct name) from the AA group by ID;
+------+-----------------------------+
| id| Group_concat (distinct name) |
+------+-----------------------------+
|1 | 10,20|
| 20 |
|3 | 200,500 |
+------+-----------------------------+
3 Rows in Set (0.00 sec)
Group by ID, print the value of the Name field on one line, comma separated, and then name in reverse order
Mysql> Select Id,group_concat (name order BY name Desc) from the AA group by ID;
+------+---------------------------------------+
| id| Group_concat (name order BY name Desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
| 20|
|3 | 500,200|
+------+---------------------------------------+
3 Rows in Set (0.00 sec)
Using the Group_concat_max_len system variable, you can set the maximum allowable length. The syntax for doing this in a program is as follows, where Val is an unsigned integer:
SET [Session | GLOBAL] Group_concat_max_len = val;
If the maximum length has been set, the result is the maximum length.
Increase the environment variable Group_concat_max_len. The default is 1024. I set the session-level environment variable to 2048 (not enough to increase it). Solve the problem
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.