mysql splits a row of data into multiple rows based on commas

mysql splits a row of data into multiple rows based on commas

Separation effect

-- Before separation
1,2,3,4
-- After separation
1
2
3
4

Separate command lines

SELECT 
 substring_index(substring_index('1,2,3,4',',', b.help_topic_id + 1), ',', -1) result
FROM 
 mysql.help_topic b
where 
 b.help_topic_id < (LENGTH('1,2,3,4') - LENGTH(REPLACE('1,2,3,4', ',', '')) + 1);

Command line explanation

help_topic itself is a Mysql help explanation comment table, which is used to explain various Mysql terms. Since the data ID of this table increases from 0, it is convenient for us to count, but the 8.0.17 version has only 686 data. If it exceeds this number, we need to customize a table

It can be used as a temporary table for counting. The query statement will only use help_topic for counting. The excess part is actually dirty data.

b.help_topic_id < Get the total number of rows after separation

b.help_topic_id < (LENGTH('1,2,3,4') - LENGTH(REPLACE('1,2,3,4', ',', '')) + 1)

This statement is actually a traversal, help_topic_id starts from 0 and increases to

Intercept in order, first intercept all characters before the nth separator, then intercept the last character of the string

1 —— 1
1,2 —— 2
1,2,3 —— 3
1,2,3,4 —— 4

mysql.help_topic no permission solution

The function of mysql.help_topic is to connect the data from the SUBSTRING_INDEX function (that is, the data separated by the delimiter) to make a Cartesian product.

If you do not have permission to use mysql.help_topic, you can create a temporary table to connect to the table you want to query.

Get the maximum number of strings that the field can be split into:

SELECT MAX(LENGTH(a.`name`) - LENGTH(REPLACE(a.`name`, ',', '' )) + 1) FROM `test` a;

Create a temporary table and add data to it:

Notice:

  • The temporary table must have a column of auto-increment data starting from 0 or 1
  • The temporary table name is arbitrary, and there can be only one field
  • The amount of data to be temporarily represented must be greater than MAX(LENGTH(a.name) - LENGTH(REPLACE(a.name, ',', '' )) + 1)

Involved functions

substring_index(str, delim, count)

Parameter name explain
str The string to be split
delim Separator, split by a character
count When count is a positive number, all characters before the nth delimiter are taken; when count is a negative number, all characters after the nth delimiter from the end are taken.

replace(str, from_str, to_str)

Parameter name explain
str The string to be replaced
from_str The string to be replaced
to_str The string to be replaced

length(str) Get the length of a string

This is the end of this article about how to split a row of data into multiple rows of data in MySQL based on commas. For more information about how to split a row of data into multiple rows in MySQL based on commas, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL string splitting example (string extraction without separator)
  • MySQL string splitting operation (string interception containing separators)
  • Vertical and horizontal splitting of MySQL tables
  • MySQL merge and split by specified characters example tutorial
  • MySQL interception and split string function usage examples
  • Summary of MYSQL database data splitting: sub-library and sub-table
  • MySQL common data splitting methods
  • Use Perl to split the data table (MySQL) and migrate the data instance

<<:  Detailed explanation of HTML onfocus gain focus and onblur lose focus events

>>:  What is ssh port forwarding? What's the use?

Recommend

About ROS2 installation and docker environment usage

Table of contents Why use Docker? Docker installa...

The core process of nodejs processing tcp connection

A few days ago, I exchanged some knowledge about ...

MySQL uses find_in_set() function to implement where in() order sorting

This article introduces a tutorial about how to u...

Full HTML of the upload form with image preview

The upload form with image preview function, the ...

Detailed explanation of Axios asynchronous communication in Vue

1. First, we create a .json file for interactive ...

How to improve Idea startup speed and solve Tomcat log garbled characters

Table of contents Preface Idea startup speed Tomc...

Summary of react basics

Table of contents Preface start React Lifecycle R...

Example code for implementing background transparency and opaque text with CSS3

Recently, I encountered a requirement to display ...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...

React Fragment Introduction and Detailed Usage

Table of contents Preface Motivation for Fragment...

How to backup MySQL regularly and upload it to Qiniu

In most application scenarios, we need to back up...

How to change MySQL character set utf8 to utf8mb4

For MySQL 5.5, if the character set is not set, t...

Markup language - specify CSS styles for text

Click here to return to the 123WORDPRESS.COM HTML ...

React non-parent-child component parameter passing example code

React is a JAVASCRIPT library for building user i...