当前位置:网站首页>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

 Insert picture description here

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 !

原网站

版权声明
本文为[Python and data analysis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040724194022.html