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

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...

Vue implements time countdown function

This article example shares the specific code of ...

Detailed explanation of the basic usage of SSH's ssh-keygen command

SSH public key authentication is one of the SSH a...

Example code of javascript select all/unselect all operation in html

Copy code The code is as follows: <html> &l...

Let's talk about the problem of Vue integrating sweetalert2 prompt component

Table of contents 1. Project Integration 1. CDN i...

CSS uses calc() to obtain the current visible screen height

First, let's take a look at the relative leng...

JavaScript implements simple date effects

The specific code of JavaScript date effects is f...

How to install mysql in docker

I recently deployed Django and didn't want to...

Detailed explanation of Vue login and logout

Table of contents Login business process Login fu...

The normal method of MySQL deadlock check processing

Normally, when a deadlock occurs, the connection ...

MySQL trigger principle and usage example analysis

This article uses examples to explain the princip...