MySQL merges a field in the record

Source: Internet
Author: User

Merge a field in a single record: Concat ()
If for the user table, the following:

Age
ID class name
1 1001 Zh 18
2 1001 En 19
3 1002 Cs 18
4 1002 Jp 19

If you want to display name and age as a field, there are:

select id, class, concat(name, ": ", age) as name_age from user;

Results:

ID class Name_age
1 1001 Zh:18
2 1001 En:19
3 1002 Cs:18
4 1002 Jp:19

Two merge some fields from multiple records: Group_coacat ()
Still on the user table above, if grouped by grade, and the name and age are all combined in one column, there are:

select class, group_concat(name, ":", age) as name_age from user group by class;

The result is:

class Name_age
1001 Zh:18,en:19
1002 Cs:18,jp:19

Using the Group_coacat () method, by default, is split with "," and if you want to split with other characters you can use "separator", such as:

select class, group_concat(name, ":", age separator ";") as name_age from user group by class;

The result is:

class Name_age
1001 Zh:18;en:19
1002 Cs:18;jp:19

MySQL merges a field in the record

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.