Mysql uses group_concat () to merge multiple rows of data into one row _ MySQL

Source: Internet
Author: User
Merge the multi-row records obtained by querying the name field, which can be implemented through a program, but can also be completed directly at the SQL layer. For more information, see the following two tables a, B, table B is associated with table a through the field id, and table a and Table B are one-to-many relationships. Assume that Table B has a field name. now you need to query the records in Table a and obtain the name information stored in Table B according to the regular query, the number of rows to be displayed. if you want to display only the number of rows in table a, you need to merge the multiple rows in the query name field. this can be achieved through a program, but it can also be completed directly at the SQL layer.

Method:

Using the group_concat () method, the parameter is the field to be merged. the default delimiter of the merged field is comma, which can be specified by the separator parameter. this method is often used together with group.

Example:

Select a. *, group_concat (B. name separator '-') as name from a left join B on a. id = B. id group by a. id;

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.