How to Use the MySQL group_concat Function

Source: Internet
Author: User

The MySQL group_concat function is a typical string connection function. The following describes the syntax of MySQL group_concat. It is helpful for you to learn MySQL group_concat function.

MySQL group_concat Function
The complete syntax is as follows:
Group_concat ([DISTINCT] field to be connected [Order by asc/DESC sorting field] [Separator 'delimiter'])

Basic Query

 
 
  1. mysql> select * from aa;  
  2. +------+------+  
  3. | id| name |  
  4. +------+------+  
  5. |1 | 10|  
  6. |1 | 20|  
  7. |1 | 20|  
  8. |2 | 20|  
  9. |3 | 200   |  
  10. |3 | 500   |  
  11. +------+------+  
  12. 6 rows in set (0.00 sec) 

Group by id and print the value of the name field in one row, separated by commas (, default)

 
 
  1. mysql> select id,group_concat(name) from aa group by id;  
  2. +------+--------------------+  
  3. | id| group_concat(name) |  
  4. +------+--------------------+  
  5. |1 | 10,20,20|  
  6. |2 | 20 |  
  7. |3 | 200,500|  
  8. +------+--------------------+  
  9. 3 rows in set (0.00 sec)  
  10.  

Group by id, print the value of name field in one row, separated by semicolons

 
 
  1. mysql> select id,group_concat(name separator ';') from aa group by id;  
  2. +------+----------------------------------+  
  3. | id| group_concat(name separator ';') |  
  4. +------+----------------------------------+  
  5. |1 | 10;20;20 |  
  6. |2 | 20|  
  7. |3 | 200;500   |  
  8. +------+----------------------------------+  
  9. 3 rows in set (0.00 sec)  
  10.  

Group by id and print the value of the redundant name field in one row,

Separated by commas

 
 
  1. mysql> select id,group_concat(distinct name) from aa group by id;  
  2. +------+-----------------------------+  
  3. | id| group_concat(distinct name) |  
  4. +------+-----------------------------+  
  5. |1 | 10,20|  
  6. |2 | 20   |  
  7. |3 | 200,500 |  
  8. +------+-----------------------------+  
  9. 3 rows in set (0.00 sec) 

Group by id, print the value of the name field in one row, and separate the values with commas (,). Sort the values by name in reverse order.

 
 
  1. mysql> select id,group_concat(name order by name desc) from aa group by id;  
  2. +------+---------------------------------------+  
  3. | id| group_concat(name order by name desc) |  
  4. +------+---------------------------------------+  
  5. |1 | 20,20,10   |  
  6. |2 | 20|  
  7. |3 | 500,200|  
  8. +------+---------------------------------------+  
  9. 3 rows in set (0.00 sec)  
  10.  


 

Use of MySQL concat Functions

MySQL primary key design principles

Advantages of MySQL partitioning

Restrictions on primary key constraints in Mysql partition tables

Example of defining MySQL transactions

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.