MySQL intercept string with reverse function

Source: Internet
Author: User
Tags mysql functions mysql index

MySQL's function Daquan:

Http://www.jb51.net/Special/606.htm

A lot of knowledge on this page, you can learn about MySQL functions, can also be used as API query:

Here we just say the intercept function and the reverse function of MySQL:

MySQL string intercept function: Left (), right (), substring (), Substring_index (). There is also mid (), substr (). where mid (), substr () is equivalent to the substring () function, the function of substring () is very powerful and flexible.

1. String intercept: Left (str, length)

Mysql> Select Left (' example.com ', 3);
+-------------------------+
| Left (' example.com ', 3) |
+-------------------------+
| Exa |
+-------------------------+
2. String intercept: Right (str, length)

Mysql> Select Right (' example.com ', 3);
+--------------------------+
| Right (' example.com ', 3) |
+--------------------------+
| com |
+--------------------------+

Instance:

#查询某个字段后两位字符
Select Right (LAST3, 2) as Last2 from Historydata limit 10;
#从应该字段取后两位字符更新到另外一个字段
Update ' historydata ' Set ' Last2 ' =right (LAST3, 2);


3. String interception: Substring (str, POS); SUBSTRING (str, POS, len)

3.1 starts at the 4th character position of the string until the end.

mysql> Select substring (' example.com ', 4);
+------------------------------+
| SUBSTRING (' example.com ', 4) |
+------------------------------+
| mple.com |
+------------------------------+
3.2 starts at the 4th character position of the string and takes only 2 characters.

mysql> Select substring (' example.com ', 4, 2);
+---------------------------------+
| SUBSTRING (' example.com ', 4, 2) |
+---------------------------------+
| MP |
+---------------------------------+
3.3 Starts from the 4th character position (reciprocal) of the string until it ends.

mysql> Select substring (' example.com ',-4);
+-------------------------------+
| SUBSTRING (' example.com ',-4) |
+-------------------------------+
|. com |
+-------------------------------+
3.4 is taken from the 4th character position (reciprocal) of the string and takes only 2 characters.

mysql> Select substring (' example.com ',-4, 2);
+----------------------------------+
| SUBSTRING (' example.com ',-4, 2) |
+----------------------------------+
|. C |
+----------------------------------+
We notice that in function substring (str,pos, len), POS can be negative, but Len cannot take a negative value.

4. String interception: Substring_index (Str,delim,count)

4.1 Intercept all characters before the second '. '.

Mysql> Select Substring_index (' www.example.com ', '. ', 2);
+------------------------------------------------+
| Substring_index (' www.example.com ', '. ', 2) |
+------------------------------------------------+
| Www.example |
+------------------------------------------------+
4.2 Intercept all characters after the second '. ' (Countdown).

Mysql> Select Substring_index (' www.example.com ', '. ',-2);
+-------------------------------------------------+
| Substring_index (' www.example.com ', '. ',-2) |
+-------------------------------------------------+
| example.com |
+-------------------------------------------------+
4.3 If the value specified by the Delim parameter is not found in the string, the entire string is returned

Mysql> Select Substring_index (' www.example.com ', '. CoC ', 1);
+---------------------------------------------------+
| Substring_index (' www.example.com ', '. CoC ', 1) |
+---------------------------------------------------+
| www.example.com |
+---------------------------------------------------+

MySQL's reverse function:

Topic:

/a/b/c/d/123.jpg

E/w/r/t/345.jpg

/2/3/5/6/567.jpg

Please use MySQL to get the last picture name:

Scenario: First use the reverse function to flip these strings

Then use the MySQL index to intercept the string

After the interception, flip it over.

Because MySQL does not like Java can intercept the last slash behind the content, so must take this way

MySQL intercept string with reverse function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.