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

How to restore single table data using MySQL full database backup data

Preface When backing up the database, a full data...

JS Asynchronous Stack Tracing: Why await is better than Promise

Overview The fundamental difference between async...

How to prevent Vue from flashing in small projects

Summary HTML: element plus v-cloak CSS: [v-cloak]...

Tutorial diagram of installing mysql8.0.18 under linux (Centos7)

1 Get the installation resource package mysql-8.0...

Access the MySQL database by entering the DOS window through cmd under Windows

1. Press win + R and type cmd to enter the DOS wi...

Summary of the use of html meta tags (recommended)

Meta tag function The META tag is a key tag in th...

Steps for encapsulating element-ui pop-up components

Encapsulate el-dialog as a component When we use ...

MySQL users and permissions and examples of how to crack the root password

MySQL Users and Privileges In MySQL, there is a d...

How to change the tomcat port number in Linux

I have several tomcats here. If I use them at the...

How to add Nginx to system services in CentOS7

Introduction After compiling, installing and solv...

HTML drawing user registration page

This article shares the specific implementation c...

Two solutions for automatically adding 0 to js regular format date and time

Table of contents background Solution 1 Ideas: Co...

react+antd.3x implements ip input box

This article shares the specific code of react+an...

Detailed explanation of Kubernetes pod orchestration and lifecycle

Table of contents K8S Master Basic Architecture P...