MySQL Group_concat function---useful one to query all group by groups after all the contents of the same groups

Source: Internet
Author: User

This article introduces the use of the Group_concat function in MySQL, such as select Group_concat (name). The complete syntax for the GROUP_CONCAT function in MySQL 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| | 2 | 20| | 3 | 200 | | 3 | |+------+------+6 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field on one line, comma separated (default) mysql> select Id,group_concat (name) from AA GROUP BY id;+------+--------------------+| id| Group_concat (name) |+------+--------------------+|1 | 10,20,20| | 2 | 20 | | 3 | 200,500|+------+--------------------+3 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field on one line, semicolon separating mysql> select Id,group_ Concat (name separator '; ') from the AA group by id;+------+----------------------------------+| id| Group_concat (name separator '; ') |+------+----------------------------------+|1 | 10;20;20 | | 2 | 20| | 3 | 200;500 |+------+----------------------------------+3 rows in Set (0.00 sec) is grouped by ID, printing the value of the de-redundant name field on one line, separated by commas mysql> Select Id,group_concat (Distinct NAMe) from the AA group by id;+------+-----------------------------+| id| Group_concat (distinct name) |+------+-----------------------------+|1 | 10,20| | 2 | 20 | | 3 | 200,500 |+------+-----------------------------+3 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field in one line, separated by commas, and reversed by name 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 | | 2 | 20| | 3 | 500,200|+------+---------------------------------------+3 rows in Set (0.00 sec)

For length setting, please see the group_concat length setting link .

Simple usage:

SET GLOBAL group_concat_max_len=102400, followed by the SQL statement containing the GROUP_CONCAT. (Global query settings, as long as the settings, this MySQL server here the length is permanently set to this value.) )
SET SESSION group_concat_max_len=102400, and then directly with the SQL statement containing the GROUP_CONCAT. (the setting of a session query will only take effect if the query is in the same session)

MySQL Group_concat function---useful one to query all group by groups after all the contents of the same groups

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.