In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to extract values from a string by MySQL". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
MySQL has so many string functions that sometimes I don't know how to use them flexibly.
String basic information function collation convert,char_length, etc.
Encryption functions password (x), encode, aes_encrypt
String concatenation function concat (x1thine x2, … (.)
Pruning function trim,ltrim,rtrim
Substring operation functions substring (XMagne start length), mid (x Magne start length)
String copy function repeat,space
String comparison function strcmp
String reverse function reverse
If you really give a scene, you might be able to take a picture of the chest.
Suppose I have the following requirements, such as registering an account in an email. The specified account starts with a number, and the content is as follows:
1234@mail.com
012345@aa.mail.com
1234mm@mail.com
1234test@mail.com
If you need to extract the numbers inside, is there any good way?
If you use string functions, one way is to use regular, or directly given conditions for filtering.
For example, replace (xxxx,right (xxx))
Another idea is to create a function or stored procedure and do the transformation in a structured way.
As a matter of fact, all of the above methods are troublesome. What else can I do? I will give two examples.
The first solution is to use the data type conversion of strings.
For example:
Mysql > select cast ('123456roomxx.com' as unsigned)
+-+
| | cast ('123456roomxx.com' as unsigned) |
+-+
| | 123456 |
+-+
1 row in set, 1 warning (0.00 sec)
We can clearly see the results and a warning.
Mysql > show warnings
+-- +
| | Level | Code | Message | |
+-- +
| | Warning | 1292 | Truncated incorrect INTEGER value: '123456 / 163.com' | |
+-- +
1 row in set (0.00 sec)
Solution 2:
This solution is simpler and has a feeling of miraculous craftsmanship.
Mysql > select-(- '123456 / 163.com')
+-+
| |-(- '123456 / 163.com') |
+-+
| | 123456 |
+-+
1 row in set, 1 warning (0.00 sec)
If there are redundant numbers in front of them, they can also be converted.
Mysql > select-(- 012345roomaa.mail.com')
+-+
| |-(- '012345roomaa.mail.com') |
+-+
| | 12345 |
+-+
1 row in set, 1 warning (0.00 sec)
That's all for "how MySQL extracts values from strings". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.