The group_concat function in MySQL. Connection field. Multiple values are displayed as one row.

Source: Internet
Author: User


The group_concat function and connection field in MySQL. The complete syntax of the group_concat function in MySQL is as follows: group_concat ([DISTINCT] field to be connected [Order by asc/DESC sorting field] [Separator 'delimiter']) basic query mysql> select * from aa; + ------ + | id | name | + ------ + | 1 | 10 | 1 | 20 | 1 | 20 | 2 | 20 | 3 | 200 | | 3 | 500 | + ------ + 6 rows in set (0.00 sec) group by id, print the value of the name field in one row, and separate them by commas (,) (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) grouped by id, print the value of the name field in one row, use semicolons to separate mysql> select id, group_concat (name separator ';') from 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) group by id and print the value of the redundant name field in one row. Separate them by commas (,) from mysql> select id, group_concat (distinct name) from aa group by id; + ------ + ----------------------------- + | id | group_concat (distinct name) | + ------ + --------------------------- + | 1 | 200,500 | 2 | 20 | 3 | 0.00 | + ------ + --------------------------- + 3 rows in set (sec) grouped by id, print the value of the name field in one row, separated by commas (,), in reverse order of name mysql> select id, group_concat (name order by name desc) from aa group by id; + ------ + ------------------------------------- + | id | group_concat (name order by name desc) | + ------ + --------------------------------------- + | 1 |, 10 | 2 | 20 | 3 | 500,200 | + ------ + --------------------------------------- + 3 rows in set (0.00 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.