How to split and merge multiple values ​​in a single field in MySQL

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display

Now we have the requirements shown in Figure 1 to Figure 2

Figure 1

Figure 2

How to do it?

The following sql:

SELECT id,GROUP_CONCAT(DISTINCT str) as str from test GROUP BY id

Related knowledge points

GROUP_CONCAT

GROUP_CONCAT([DISTINCT] Fields to be connected [Order BY ASC/DESC sorting fields] [Separator 'separator'])

Multiple values ​​split display

Now our requirements are the opposite of the first example.

From Figure 3

Figure 3

To Figure 4

Figure 4

This is a little complicated. First, we have done the processing of str in advance as shown in the figure, that is, one-to-one processing of characters + ','. Secondly, because MySQL does not have a sequence function, we need to prepare a sequence table in advance. The maximum id should be greater than the maximum number of items in a single field.

Sequence Listing

The specific sql is as follows:

SELECT t.id,SUBSTRING_INDEX(SUBSTRING_INDEX(str,',',s.id),',',-1) as str 
	from test2 t 
		join sequence s on s.id<(LENGTH(t.str)/2+1) 
			ORDER BY t.id

Related knowledge points

SUBSTRING_INDEX(str,delim,count)

SUBSTRING_INDEX(string to process, separator, count)

If count is a positive number, then it is all the content to the left of the Nth delimiter, counting from left to right.

If it is a negative number, then it starts from the right and all the contents to the right of the Nth separator are counted.

Summarize

This is the end of this article about MySQL single field multi-value splitting and merging processing. For more relevant MySQL single field multi-value splitting and merging content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to assign default values ​​to fields when querying MySQL
  • Add a field to the table in the MySQL command line (field name, whether it is empty, default value)
  • MySQL table field setting default value (graphic tutorial and pay attention to details)
  • Detailed example of concatenating multiple fields in mysql
  • In-depth explanation of hidden fields, a new feature of MySQL 8.0
  • A brief discussion on which fields in Mysql are suitable for indexing
  • mysql update case update field value is not fixed operation
  • When modifying a record in MySQL, the update operation field = field + string
  • Detailed analysis of the syntax of Mysql update to modify multiple fields and
  • How to set the default value of a MySQL field

<<:  HTML table tag tutorial (44): table header tag

>>:  Method of building redis cluster based on docker

Recommend

Solution to the problem of session failure caused by nginx reverse proxy

A colleague asked for help: the login to the back...

Mini Program to Implement the Complete Shopping Cart

The mini program implements a complete shopping c...

Binary Type Operations in MySQL

This article mainly introduces the binary type op...

Docker-compose quickly builds steps for Docker private warehouse

Create docker-compose.yml and fill in the followi...

MySQL aggregate function sorting

Table of contents MySQL result sorting - Aggregat...

8 powerful techniques for HTML web page creation

<br />Although there are many web page creat...

Causes and solutions to the garbled character set problem in MySQL database

Preface Sometimes when we view database data, we ...

Problems and solutions when replacing Oracle with MySQL

Table of contents Migration Tools Application tra...

Graphical explanation of the solutions for front-end processing of small icons

Preface Before starting this article, let’s do a ...

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

Summary of several replication methods for MySQL master-slave replication

Asynchronous replication MySQL replication is asy...

How to hide rar files in pictures

You can save this logo locally as a .rar file and...

Share the 15 best HTML/CSS design and development frameworks

Professional web design is complex and time-consu...