PrefaceNote: 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. DemandWe 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. SolutionOracle 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) SummarizeThis 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
1. Percentage basis for element width/height/padd...
In order to solve the problem mentioned last time...
Hello everyone, today we will talk about how to u...
Table of contents 1. Understanding the Equality R...
1.Tomcat Optimization Configuration (1) Change To...
We all know that we can use the mkdir command to ...
1. Download and install MySql Download MySql data...
This article shares with you how to use Vue to dr...
Table of contents Forward Proxy nginx reverse pro...
Table of contents 1. What is the life cycle 2. Lo...
1 Start the Docker service First you need to know...
Original text: https://dev.mysql.com/doc/refman/8...
The default template method is similar to vue2, u...
React native implements the monitoring gesture to...
Table of contents What is Routing Basic use of pu...