How does mysql merge multi-row query results into one row?

Source: Internet
Author: User

When a single ID corresponds to multiple names using the function group_concat (), the name is merged into one row for multiple rows of data.

Complete Syntax:

GROUP_CONCAT (expr)

This function returns a string with a non-NULL value from a group of connections. The complete syntax is as follows:

GROUP_CONCAT ([DISTINCT] expr [, expr...]

[Order by {unsigned_integer | col_name | expr}

[ASC | DESC] [, col_name...]

[SEPARATOR str_val])

Mysql> SELECT student_name,

-> GROUP_CONCAT (test_score)

-> FROM student

-> Group by student_name;

Or:

Mysql> SELECT student_name,

-> GROUP_CONCAT (DISTINCT test_score

-> Order by test_score desc separator '')

-> FROM student

-> Group by student_name;

In MySQL, you can obtain the join value of expression combinations. You can use DISTINCT to delete duplicate values. If you want to sort multiple result values, use the order by clause. To sort in reverse ORDER, add the DESC (descending) keyword to the column name that you want to sort using the order by clause. The default order is ascending. You can use ASC to specify it. SEPARATOR follows the string value in the middle of the value of the inserted result. The default value is comma (','). You can delete all separators by specifying the SEPARATOR.

Using group_concat_max_len system variables, you can set the maximum allowed length. The syntax for this operation in the program is as follows, where val is an unsigned integer:

SET [SESSION | GLOBAL] group_concat_max_len = val;

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.