MySQL merges multiple rows of data based on the group_concat() function

MySQL merges multiple rows of data based on the group_concat() function

A very useful function

group_concat(), the manual states: This function returns a string result with non-NULL values ​​from a group connection.

To put it simply, group_concat() will calculate which rows belong to the same group and merge the columns that belong to the same group and display them together. Which columns to return is determined by the function parameters (that is, the field names). There must be a standard for grouping, that is, grouping according to the column specified by group by.

The default separator for merged fields is a comma, which can be specified using the separator parameter.

For example, in the student table, there are 5 pieces of data as follows:

The requirements are as follows: "Xiao Ming"'s two rows of scores can be displayed on one row, and "Xiao Hong"'s two rows of scores can also be displayed on one row!

Then you can use

SELECT name,group_concat(subject,score) FROM student group by name;

The query results are as follows:

You can also customize the delimiter

SELECT name,group_concat(subject,score separator '--') FROM student group by name;

The query results are as follows:

What happens if you don't use group by name;?

SELECT name,group_concat(subject,score) FROM student;

The query results are as follows (only one row is shown, which has nothing to do with the name attribute):

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • mysql group_concat method example to write group fields into one row
  • MySql Group By implements grouping of multiple fields
  • mysql group by grouping multiple fields
  • How to merge and display multiple data of a field after grouping in MySQL

<<:  How to define data examples in Vue

>>:  Web project development JS function anti-shake and throttling sample code

Recommend

...

Detailed explanation of MySQL InnoDB secondary index sorting example

Sorting Problem I recently read "45 Lectures...

HTML+CSS project development experience summary (recommended)

I haven’t updated my blog for several days. I jus...

Configure selenium environment based on linux and implement operation

1. Using Selenium in Linux 1. Install Chrome Inst...

Detailed explanation of the use of props in React's three major attributes

Table of contents Class Component Functional Comp...

MySQL Full-text Indexing Guide

Full-text indexing requires special query syntax....

Unicode signature BOM (Byte Order Mark) issue for UTF-8 files

I recently encountered a strange thing when debug...

Use Vue3 to implement a component that can be called with js

Table of contents Preface 1. Conventional Vue com...

Vue's vue.$set() method source code case detailed explanation

In the process of using Vue to develop projects, ...

MySQL 5.7.17 installation and configuration tutorial under Linux (Ubuntu)

Preface I have installed MySQL 5.6 before. Three ...

Solve the problem that await does not work in forEach

1. Introduction A few days ago, I encountered a p...

WeChat applet tab left and right sliding switch function implementation code

Effect picture: 1. Introduction Your own applet n...

Docker runs operations with specified memory

as follows: -m, --memory Memory limit, the format...