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

Introduction to using the MySQL mysqladmin client

Table of contents 1. Check the status of the serv...

MySQL 5.7.21 Installer Installation Graphic Tutorial under Windows 10

Install MySQL and keep a note. I don’t know if it...

Implementation of Docker CPU Limit

1. --cpu=<value> 1) Specify how much availa...

Summary of flex layout compatibility issues

1. W3C versions of flex 2009 version Flag: displa...

Summary of events that browsers can register

Html event list General Events: onClick HTML: Mous...

Solution to css3 transform transition jitter problem

transform: scale(); Scaling will cause jitter in ...

Detailed explanation of the minimum width value of inline-block in CSS

Preface Recently, I have been taking some time in...

Use a few interview questions to look at the JavaScript execution mechanism

Table of contents Previous words Synchronous and ...

10 very good CSS skills collection and sharing

Here, clever use of CSS techniques allows you to g...

Detailed explanation of MySQL multi-table join query

Table of contents Multi-table join query Inner Jo...

How to use vue-cli to create a project and package it with webpack

1. Prepare the environment (download nodejs and s...

Detailed explanation of the pitfalls of MySQL 8.0

I updated MySQL 8.0 today. The first problem: Nav...

IE8 provides a good experience: Activities

Today I had a sneak peek at IE8 beta 1 (hereafter...

Linux bridge method steps to bridge two VirtualBox virtual networks

This article originated from my complaints about ...