MySQL merge values for the same column

Source: Internet
Author: User

I like the following query condition shows:

I hope the effect of the final display

Merge the group_name with the same ID into one line. Implementation method: Add the keyword group_concat in front of group_name. Then add the group by ID at the end of the SQL statement.

Group_concat can separate the values of the same column into a string by separating them with commas. (MySQL comes with)

The SQL statement for the first diagram is as follows:

SELECT tr.id, tig.group_name from Tj_record tr
INNER JOIN tj_item_group_ex tige on tr.id = tige.record_id
INNER JOIN tj_item_group tig on tige.id = tig.id
WHERE tr.id in (' 1407090008 ', ' 1407090009 ') and tige.id in (' 27 ', ' 31 ')


The SQL statement for the second diagram is as follows:

SELECT tr.id,group_concat (tig.group_name) from Tj_record tr
INNER JOIN tj_item_group_ex tige on tr.id = tige.record_id
INNER JOIN tj_item_group tig on tige.id = tig.id
WHERE tr.id in (' 1407090008 ', ' 1407090009 ') and tige.id in (' 27 ', ' 31 ')

GROUP by Tr.id

Red is the difference part. If that's the case, it's no longer necessary to have a single spelling string in the background.

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.