The use of the Group_concat function in MySQL

Source: Internet
Author: User

1 This article introduces the use of the Group_concat function in MySQL, such as select Group_concat (name). 2 group_concat function in MySQL3 the complete syntax is as follows:4Group_concat ([DISTINCT] field to connect [order by Asc/desc sort field] [Separator'Separators']) 5 Basic Query6Mysql>Select* fromAA;7+------+------+8| id| name |9+------+------+Ten|1|Ten| One|1| -| A|1| -| -|2| -| -|3| $| the|3| -| -+------+------+ - 6Rowsinch Set(0.00sec) - GROUP by ID, print the value of the Name field on one line, comma separated (default) +Mysql>SelectId,group_concat (name) fromAA Group by ID; -+------+--------------------+ +| id| Group_concat (name) | A+------+--------------------+ at|1|Ten, -, -| -|2| -| -|3| $, -| -+------+--------------------+ - 3Rowsinch Set(0.00sec) - GROUP by ID, print the value of the Name field on one line, semicolon delimited inMysql>SelectId,group_concat (Name Separator';') fromAA Group by ID; -+------+----------------------------------+ to| id| Group_concat (Name Separator';') | ++------+----------------------------------+ -|1|Ten; -; -| the|2| -| *|3| $; -| $+------+----------------------------------+Panax Notoginseng 3Rowsinch Set(0.00sec) - GROUP by ID, print the value of the redundant Name field to a line, the comma Delimited +Mysql>SelectId,group_concat (distinct name) fromAA Group by ID; A+------+-----------------------------+ the| id| Group_concat (distinct name) | ++------+-----------------------------+ -|1|Ten, -| $|2| -| $|3| $, -| -+------+-----------------------------+ - 3Rowsinch Set(0.00sec) the GROUP by ID, print the value of the Name field in one line, comma separated, and reverse by name -Mysql>SelectId,group_concat (name order BY name Desc) fromAA Group by ID;Wuyi+------+---------------------------------------+ the| id| Group_concat (name order BY name Desc) | -+------+---------------------------------------+ Wu|1| -, -,Ten| -|2| -| About|3| -, $| $+------+---------------------------------------+ - 3Rowsinch Set(0.00sec) - 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; A if the maximum length has been set, the result is up to this 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)

The use of the Group_concat function in MySQL

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.