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

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

Detailed steps to install RabbitMQ in docker

Table of contents 1. Find the mirror 2. Download ...

Detailed example of removing duplicate data in MySQL

Detailed example of removing duplicate data in My...

CentOS7 deployment Flask (Apache, mod_wsgi, Python36, venv)

1. Install Apache # yum install -y httpd httpd-de...

Python 3.7 installation tutorial for MacBook

The detailed process of installing python3.7.0 on...

How to configure Nginx domain name rewriting and wildcard domain name resolution

This article introduces how to configure Nginx to...

Cross-browser development experience summary (I) HTML tags

Add a DOCTYPE to the page Since different browser...

Vue integrates Tencent TIM instant messaging

This article mainly introduces how to integrate T...

How to manage users and groups when running Docker

Docker is a management tool that uses processes a...

A Brief Analysis of MySQL PHP Syntax

Let's first look at the basic syntax of the c...

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...

Specific use of Bootstrap5 breakpoints and containers

Table of contents 1. Bootstrap5 breakpoints 1.1 M...

Three methods of inheritance in JavaScript

inherit 1. What is inheritance Inheritance: First...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...