MySQL uses Group_concat to combine multiple data display methods

Source: Internet
Author: User

Situation Analysis:

1. Table Course

ID Name

1 Lesson One

=================

2. Table Course_teacher

ID course_id teacher_name

1 1 Teacher One

2 1 Teacher Two

====================

A statement is required to isolate the course ID, course name, teacher name

Using the Group_concat () function,

Method: Select C.*,group_concat (t.teacher_name) From course C left joins Course_teacher T on c.id=t.course_id where c.id=1

Default "," split, can also modify Group_concat (T.teacher_name SEPARATOR "-")

Note that the low version of MySQL will appear to return blobs, in order to be compatible with the old version of MySQL we need to convert the Teacher_name field

such as: Group_concat (CAST (t.teacher_name as Char) SEPARATOR "-")

MySQL uses Group_concat to combine multiple data display methods

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.