Mysql merges values of the same column. mysql merges values of the same column.

Source: Internet
Author: User

Mysql merges values of the same column. mysql merges values of the same column.

I have the following query conditions:

I want the final display Effect

Merge group_name with the same id into one row. Implementation Method: add the keyword group_concat before 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 by commas and form a string. (Mysql)

The SQL statement in the first figure 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 ('20140901', '20160901') AND tige. id IN ('27', '31 ')


The SQL statement in the second figure 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 ('20140901', '20160901') AND tige. id IN ('27', '31 ')

Group by tr. id

The red marked red is the difference. In this case, you do not need to splice strings one by one in the background.


Mysql merges records with the same field values in a table.

Select m1, sum (m2) from ABC group by m1;
 
How does Mysql merge column values?

SELECT GROUP_CONCAT (name SEPARATOR '') AS name FROM

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.