Mysql merges multiple rows of data into one row using Group_concat () _mysql

Source: Internet
Author: User

Suppose that the two table a,b,b is associated with a table through a field ID, and a table has a one-to-many relationship with Table B. Assuming that a field name exists in table B, you now need to query the records in table A. Get the name information stored in table B, as usual, how many rows are displayed in table B, and if you need to show only the number of rows in table A, you need to merge the multiline records from the Query Name field. Can be implemented programmatically, but can also be done directly at the SQL layer.

Method:

Using the Group_concat () method, the parameter is a field that needs to be merged, and the merged field is delimited Fummer think of the comma, which can be specified by parameter separator, which is often used with group by.

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.