MySQL's group_concat function

Source: Internet
Author: User

Generate the test table and populate the data according to the following statement

CREATE TABLE Z (a int, b int); INSERT into Z select 1,200;insert to Z select 1,100;insert into z select 1,100;insert INT O Z Select 2,400;insert into z select 2,500;insert to Z select 3,null;
Next, group By column A, and use the aggregate function Group_concat,sql statement as follows
SELECT A, Group_concat (b) from Zgroup by A;
The results of the operation are as follows in addition, the Group--concat aggregation function has some other uses, with the following syntax:
Group_concat ([DISTINCT] expr [, expr ...] [ORDER by {unsigned_integer | col_name | expr}[asc | DESC] [, col_name ...] [SEPARATOR Str_val])

The DISTINCT option removes duplicate values, the order selection can sort the columns, and the separator is used to select the stitching string. For example, in the example above, the column B can be de-weighed and sorted in descending order, with ":" Stitching the string. Its SQL statement can be written as:

SELECT A,group_concat (DISTINCT b ORDER by B DESC SEPARATOR ': ') from Zgroup to A;

Running results such as

MySQL's group_concat function

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.