How to extract string elements from non-fixed positions in MySQL

How to extract string elements from non-fixed positions in MySQL

Preface

Note: The test database version is MySQL 8.0

Test data:

create table zqs(id int,str varchar(1000));

insert into zqs(id,str) values ​​(1,'【JD.com】abc【China Telecom】');
insert into zqs(id,str) values ​​(1,'【JD.com】abc【China Telecom】def');
insert into zqs(id,str) values ​​(1,'****【JD.com】abc【China Telecom】def');
insert into zqs(id,str) values ​​(1,'****【JD.com】abc');
insert into zqs(id,str) values ​​(1,'【JD.com】abc【China Telecom】【China Unicom】');

1. Demand

We often encounter similar requirements as SMS, and need to extract SMS tag information, but there may be multiple SMS tags.

This example assumes that there are at most 3 tags and needs to be output as follows:

mysql> select * from zqs;
±-----±----------------------------------------------------+
| id | str |
±-----±----------------------------------------------------+
| 1 | 【JD.com】abc【China Telecom】 |
| 1 | 【JD.com】abc【China Telecom】def |
| 1 | ****【JD.com】abc【China Telecom】def |
| 1 | ****【JD.com】abc |
| 1 | 【JD.com】abc【China Telecom】【China Unicom】 |
±-----±----------------------------------------------------+

The required output is as follows:

±-------------±-------------------±-------------------+
| first_val | first_va2 | first_va3 |
±-------------±-------------------±-------------------+
| 【JD.com】 | 【China Telecom】 | |
| 【JD.com】 | 【China Telecom】 | |
| 【JD.com】 | 【China Telecom】 | |
| 【JD.com】 | | |
| 【JD.com】 | 【China Telecom】 | 【China Unicom】 |
±-------------±-------------------±-------------------+

2. Solution

Oracle string interception functions substr and instr can be used together, but MySQL's instr function is weaker than Oracle's instr function.

At this time, you need to use the MySQL regular expression regexp_instr function and substr function

select substr(str,
   regexp_instr(str,'【',1,1),
   regexp_instr(str,'】',1,1) - regexp_instr(str,'【',1,1) + 1 ) first_val, 
  substr(str,
   regexp_instr(str,'【',1,2),
   regexp_instr(str,'】',1,2) - regexp_instr(str,'【',1,2) + 1) first_va2, 
  substr(str,
   regexp_instr(str,'【',1,3),
   regexp_instr(str,'】',1,3) - regexp_instr(str,'【',1,3) + 1) first_va3
 from zqs;

Testing Log:

mysql> select substr(str,
 -> regexp_instr(str,'【',1,1),
 -> regexp_instr(str,'】',1,1) - regexp_instr(str,'【',1,1) + 1 ) first_val,
 -> substr(str,
 -> regexp_instr(str,'【',1,2),
 -> regexp_instr(str,'】',1,2) - regexp_instr(str,'【',1,2) + 1) first_va2,
 -> substr(str,
 -> regexp_instr(str,'【',1,3),
 -> regexp_instr(str,'】',1,3) - regexp_instr(str,'【',1,3) + 1) first_va3
 -> from zqs;
+--------------+--------------------+--------------------+
| first_val | first_va2 | first_va3 |
+--------------+--------------------+--------------------+
| 【JD.com】 | 【China Telecom】 | |
| 【JD.com】 | 【China Telecom】 | |
| 【JD.com】 | 【China Telecom】 | |
| 【JD.com】 | | |
| 【JD.com】 | 【China Telecom】 | 【China Unicom】 |
+--------------+--------------------+--------------------+
5 rows in set (0.00 sec)

Summarize

This is the end of this article about how to extract string elements from non-fixed positions in MySQL. For more information about extracting string elements from MySQL, 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!

<<:  Vue implements a simple marquee effect

>>:  Example code for implementing ellipse trajectory rotation using CSS3

Recommend

Detailed steps for building Portainer visual interface with Docker

In order to solve the problem mentioned last time...

Basic implementation method of cross-component binding using v-model in Vue

Hello everyone, today we will talk about how to u...

Understanding JavaScript prototype chain

Table of contents 1. Understanding the Equality R...

Tomcat+Mysql high concurrency configuration optimization explanation

1.Tomcat Optimization Configuration (1) Change To...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

mysql8.0.23 msi installation super detailed tutorial

1. Download and install MySql Download MySql data...

The vue project realizes drawing a watermark in a certain area

This article shares with you how to use Vue to dr...

Detailed explanation of nginx forward proxy and reverse proxy

Table of contents Forward Proxy nginx reverse pro...

Detailed description of the life cycle of React components

Table of contents 1. What is the life cycle 2. Lo...

How to enter and exit the Docker container

1 Start the Docker service First you need to know...

Briefly describe mysql monitoring group replication

Original text: https://dev.mysql.com/doc/refman/8...

Example code comparing different syntax formats of vue3

The default template method is similar to vue2, u...

React native realizes the monitoring gesture up and down pull effect

React native implements the monitoring gesture to...

Teach you about react routing in five minutes

Table of contents What is Routing Basic use of pu...