当前位置:网站首页>Text processing function sorting in mysql, quick search of collection
Text processing function sorting in mysql, quick search of collection
2022-07-04 07:26:00 【Python and data analysis】
Hello everyone , I'm Xiangyu !
Preface
I've arranged for a while today MySQL Text processing function in , Of course, if Xiangyu arranges and misses the message on the backstage . No matter in which programming language , The processing of text is extremely important , Because everyone knows , In daily data , Texts will account for a large part , therefore , The bosses directly encapsulate the commonly used processing operations , In this way, you don't need to write your own functions when processing text , And in fact, it also gives a friendly feeling to many new friends , For example, many data analysts
Study SQL Just want to take a number , It's not a view to learn at all 、 stored procedure 、 function , So at this point , If you can use encapsulated built-in functions directly , That's the gospel of my friends , Said so much , That is to say, text processing functions are very important , Of course , Xiangyu has tidied up here , You can collect , When you need it, just check it quickly , Don't bother, you can also try to tidy up like Xiangyu .
1. Mind mapping

2. Using examples
The following example does not specifically look up the table , Direct use select To test .
2.1 Interception and splicing
left: Left hand intercept , From the front ( On the left ) Intercept a certain length of substring for the string backwards , grammar :left( character string , Interception length )
select left(' The Yangtze River flows east ',4); -- return “ Rolling Yangtze River ”
right: Right intercept , From the back ( On the right ) Truncate a certain length of substring to the string forward , grammar :right( character string , Interception length )
select right(' The Yangtze River flows east ',4); -- return “ The east of the river is flowing ”
substring : Intercept the substring with specified position and length for the string , grammar :substring( character string , From the first few characters [, Interception length ])
Be careful : The grammatical [] Indicates that this parameter can be defaulted
The third parameter of this function can be defaulted ( No ), By default, it means to intercept the last character
select substring(' Rolling Yangtze River board ',3); -- return “ Changjiang board of directors ”
select substring(' The rolling Yangtze River is full of water ',3,2); -- return “ Yangtze River ”
concat: String splicing , grammar :concat( character string 1[, character string 2, character string 3,…])
The parameters of this function can be given to one or more , When the parameter has null when , The result also returns null
select concat(' Rolling ') -- return “ Rolling ”
select concat(' Rolling ',null) -- return null
select concat(' Rolling ',' Yangtze River ',' East death ',' water ') -- return “ The Yangtze River flows east ”
2.2 transformation
lower: Convert to lowercase , grammar :lower( String to be converted )
select lower('HELLO,WORLD'); -- return “hello,world”
upper : Convert to uppercase , grammar :upper( String to be converted )
select upper('hello,world'); -- return "HELLO,WORLD"
2.3 length
length : Returns the length of the string , grammar :length( character string )
select length(' The Yangtze River flows east '); -- return 21( One Chinese character takes up the length of three English characters )
select length('hello'); -- return 5
select length(null) -- return null
2.4 Find the substring position
locate: When a substring can be found in the original string, its first position in the original string is returned , grammar Locate( Substring , Parent string )
explain : The substring does not exist and returns 0, The substring is null return null
select locate('sql',' Xiangyu is learning mysql Or what kind sql'); -- return 8, first s Is the eighth character
select locate('pgsql',' Xiangyu is learning mysql Or what kind sql'); -- return 0
select locate(null,' Xiangyu is learning mysql Or what kind sql'); -- return null
position : and locate Function as , The writing is different , grammar position(substr in str)
select position('sql' in ' Xiangyu is learning mysql Or what kind sql'); -- return 8
instr: The function is the same as the previous two , The writing is different , grammar :instr(str,substr)
select instr(' Xiangyu is learning mysql Or what kind sql','sql'); -- return 8
2.5 Go to space
ltrim: Remove the leading space in the string , Is to remove the blank space at the beginning , grammar :ltrim( character string )
select ltrim(' It's early morning 2 spot '); -- return “ It's early morning 2 spot ”
rtrim: Remove trailing space from string , grammar :rtrim( character string )
select rtrim(' It's two in the morning '); -- return “ It's two in the morning ”
trim: Remove leading and ending spaces in the string , But it can't remove the space in the middle , grammar :trim( character string )
select trim(' Now it is In the morning At two o 'clock '); -- return “ Now it is In the morning At two o 'clock ”
2.6 fill
lpad: Returns a fixed length string , Interception beyond a fixed length , Below the fixed length, use the specified characters Left fill , grammar :lpad( character string , Fixed length , The characters that need to be filled in )
Below the fixed length
select lpad(' One gao Nest gaogao',20,'*'); -- return "******** One gao Nest gaogao"
When the fixed length is exceeded
select lpad(' Frenchman Zhang San ',4,'^^'); -- return “ Outlaw maniac ”
rpad: Returns a fixed length string , Interception beyond a fixed length , Below the fixed length, use the specified characters Right fill , grammar :rpad( character string , Fixed length , The characters that need to be filled in )
Below the fixed length
select rpad(' One gao Nest gaogao',20,'*'); -- return " One gao Nest gaogao********"
When the fixed length is exceeded
select rpad(' Frenchman Zhang San ',4,'^^'); -- return “ Outlaw maniac ”
2.7 Replace
replace: Replace the substring in the string with a new substring and return , grammar :replace( Original string , Substring to be replaced , Substring for replacement )
select replace(' The Yangtze River flows east ',' roll ',' Don't get out '); -- return “ Don't roll, don't roll, the Yangtze River flows eastward ”
Be careful : It will be completely replaced when replacing , It is not allowed to set the substring that meets the conditions
2.8 Return to pronunciation
soundex: Returns the phonetic representation of a string soundex, It helps to compare words with different spelling but similar English pronunciation , grammar :soundex( character string )
for example :
If a file named Y.LEE Wrong input when searching for customers , Below sql There will be no return results .
SELECT CUSTOMER_NAME FROM CUSTOMER WHERE CUSTOMER_NAME = 'Y LEE'
And if you write that :
SELECT CUSTOMER_NAME FROM CUSTOMER WHERE SOUNDEX(CUSTOMER_NAME) =SOUNDEX('Y LEE')
Because they sound similar , So their SOUNDEX Values match , This will return a piece of data .
soundex Refer to :https://www.cnblogs.com/shuoli/p/8099212.html
Okay ,sql The text function of is summarized here , Welcome to pay attention to add collection , At the same time, you are also welcome to Xiangyu's official account of the same name , Xiangyu is constantly updating !
边栏推荐
- 2022-021ARTS:下半年开始
- Zephyr 學習筆記2,Scheduling
- 《剑指Offer》第2版——力扣刷题
- Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
- 在所有SwiftUI版本(1.0-4.0)中原生实现Charts图表视图之思路
- Campus network problems
- Novel website program source code that can be automatically collected
- Status of the thread
- Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
- Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
猜你喜欢

Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown

Data double write consistency between redis and MySQL

Selenium ide plug-in download, installation and use tutorial
![[C language] open the door of C](/img/e0/2f107966423d6492c39995c77a445e.jpg)
[C language] open the door of C

Uniapp applet subcontracting

节点基础~节点操作

电脑通过Putty远程连接树莓派

Pangu open source: multi support and promotion, the wave of chip industry

Vulhub vulnerability recurrence 76_ XXL-JOB

果果带你写链表,小学生看了都说好
随机推荐
[MySQL transaction]
The crackdown on Huawei prompted made in China to join forces to fight back, and another enterprise announced to invest 100 billion in R & D
Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
SQL foundation 9 [grouping data]
notepad++如何统计单词数量
University stage summary
Centos8 install mysql 7 unable to start up
Lottery system test report
com. alibaba. nacos. api. exception. NacosException
The difference between synchronized and lock
2022 - 021arts: début du deuxième semestre
Bottom problem of figure
What is the use of cloud redis? How to use cloud redis?
Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
Implementation of ZABBIX agent active mode
大学阶段总结
2022-021ARTS:下半年開始
Status of the thread
Adaptive spatiotemporal fusion of multi-target networks for compressed video perception enhancement
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction