Mysql example of splitting into multiple rows and columns by specific symbols

Mysql example of splitting into multiple rows and columns by specific symbols

Some fault code tables use the following design pattern for historical or performance reasons. That is, multiple attribute values ​​are stored in the same row or column. As shown in the tonly_error_record in the following table:


In this case, you can consider splitting the column by semicolon ";" to form multiple rows, and then splitting it by comma "," to form multiple columns. As shown in the following table:

insert image description here

This can be achieved using the string splitting function in MySQL. The function description is as follows:

SUBSTRING_INDEX(str,delim,count)   
-- str: the string to be split; delim: the delimiter; count: the number of times the delimiter appears

Finally, the specific implementation is as follows:

#Step 1: Split into multiple rows based on semicolon ";" #Step 2: Split into multiple columns based on comma "," select distinct S1.tbox_vin,
                (select substring_index(substring_index(S1.error_code, ',', 1), ',', -1)) as spn,
                (select substring_index(substring_index(S1.error_code, ',', 2), ',', -1)) fmi,
                S1.modify_time
from (
         select t1.tbox_vin,
                substring_index(substring_index(t1.dm1_string, ';', t2.help_topic_id + 1), ';', -1) as error_code,
                t1.modify_time
         from tonly_error_record t1
                  join mysql.help_topic t2
                       on t2.help_topic_id < (length(t1.dm1_string) - length(replace(t1.dm1_string, ';', '')) + 1)
         where t1.dm1_string is not null
           and t1.dm1_string != '') S1
where s1.error_code != ''
  and s1.error_code is not null
order by S1.modify_time desc;

Knowledge points involved

1. String splitting: SUBSTRING_INDEX (str, delim, count)

1. Parameter explanation

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.

2. Examples

(1) Get all the characters before the second comma "," as the separator.

SUBSTRING_INDEX('7654,7698,7782,7788',',',2)

獲取第2個以“,”逗號為分隔符之前的所有字符

(2) Get all characters after the second to last comma separator ","

SUBSTRING_INDEX('7654,7698,7782,7788',',',-2) 

獲取倒數第2個以“,”逗號分隔符之后的所有字符

2. Replacement function: replace(str, from_str, to_str)

1. Parameter explanation

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

2. Examples

(1) Replace the separator "," comma with "" space.

REPLACE('7654,7698,7782,7788',',','')

將分隔符“,”逗號替換為“”空

3. Get the string length: LENGTH( str )

1. Parameter explanation

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

2. Example (1) Get the length of the string '7654,7698,7782,7788'

LENGTH('7654,7698,7782,7788') 

獲取 '7654,7698,7782,7788' 字符串的長度

Implemented SQL parsing

SELECT 
    SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num 
FROM 
    mysql.help_topic 
WHERE 
    help_topic_id < LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1

Here, help_topic_id of the help_topic table of the MySQL library is used as a variable because help_topic_id is auto-incrementing. Of course, auto-incrementing fields of other tables can also be used as an aid.

help_topic table:

help_topic 表

Implementation steps:

Step 1: First, get the number of strings that need to be split in the end, and use help_topic_id to simulate traversing the nth string.

The code snippet involved:

help_topic_id < LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1

實現過程

Step 2: Split the string based on the comma "," using the SUBSTRING_INDEX (str, delim, count) function, and assign the result to the num field.

The code snippet involved:

SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num

first step:

Use the comma "," as the delimiter and intercept all the strings before the n+1th delimiter according to the value of help_topic_id. (Here n+1 is because help_topic_id starts from 0, and here we need to get it from the first separator.)

SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1)

For example:
When help_topic_id = 0, the obtained string = 7654
When help_topic_id = 1, the obtained string = 7654,7698
…(and so on)

Step 2:

Use the comma "," as the delimiter and extract all the strings after the last delimiter.

SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1)

For example:
According to the first step, when help_topic_id = 0, the obtained string = 7654, at this time the string intercepted in the second step = 7654
According to the first step, when help_topic_id = 1, the obtained string = 7654,7698, at this time the string intercepted in the second step = 7698
…(and so on)

Finally, the following effects were successfully achieved~

這里寫圖片描述

Note: For string splitting without delimiters, please refer to MySQL - String Splitting (String Interception without Delimiters)

This is the end of this article about examples of MySQL splitting into multiple rows and columns by specific symbols. For more information about MySQL specific symbol splitting, 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 split and merge multiple values ​​in a single field in MySQL
  • MySql import CSV file or tab-delimited file
  • Mysql splits string into array through stored procedure
  • Implementing a SPLIT-like string splitting function in MySQL

<<:  A nice html printing code supports page turning

>>:  Difference between src and href attributes

Recommend

The front-end page pop-up mask prohibits page scrolling

A problem that front-end developers often encount...

Linux method example to view all information of the process

There is a task process on the server. When we us...

JS object copying (deep copy and shallow copy)

Table of contents 1. Shallow copy 1. Object.assig...

MySQL 8.0.20 installation tutorial and detailed tutorial on installation issues

Original address: https://blog.csdn.net/m0_465798...

Vue component communication method case summary

Table of contents 1. Parent component passes valu...

Vue sample code for implementing two-column horizontal timeline

Table of contents 1. Implement the component time...

JavaScript canvas Tetris game

Tetris is a very classic little game, and I also ...

Vue implements a simple calculator

This article example shares the specific code of ...

How to implement scheduled backup of CentOS MySQL database

The following script is used for scheduled backup...

Usage of MySQL time difference functions TIMESTAMPDIFF and DATEDIFF

Usage of time difference functions TIMESTAMPDIFF ...

XHTML introductory tutorial: Use of list tags

Lists are used to list a series of similar or rela...

JS generates unique ID methods: UUID and NanoID

Table of contents 1. Why NanoID is replacing UUID...

CentOS system rpm installation and configuration of Nginx

Table of contents CentOS rpm installation and con...

JavaScript implements click toggle function

This article example shares the specific code of ...