Mysql intercept and split string function usage Example _mysql

Source: Internet
Author: User

The examples in this article describe MySQL interception and split string function usage. Share to everyone for your reference, specific as follows:

First of all, intercept String functions:

SUBSTRING (commentid,9)

This is very simple, from the 9th character to intercept to the last. The substring has three parameters, the last is the length of the Intercept, the default is to the end, and the negative number is the penultimate.

Next, split String functions:

Substring_index (Commentid, '-', 1)

This is a little more complicated, he means to split the string, starting with the first keyword to take all the previous strings. If the third argument above is modified to-1, then the first is the penultimate one. If we want to get 50 or 11065 out of the string c-11065-50, how do we write it?

Get 50 of the wording:

Copy Code code as follows:
SELECT Substring_index (Checkid, '-', -1) from ' check ' WHERE checkid = ' c-11065-50 '

Get 11065 of the wording:
Copy Code code as follows:
SELECT Substring_index (Substring_index (Checkid, '-', -2), '-', 1) from check WHERE checkid = ' c-11065-50 '

Or:
Copy Code code as follows:
SELECT Substring_index (Substring_index (Checkid, '-', 2), '-', -1) from check WHERE checkid = ' c-11065-50 '

It looks more complicated, so a little more complicated:

Here is the combination of usage, such as we want to intercept: 13 of content_13-11220-1, the simplest is:

Copy Code code as follows:
SELECT Substring_index (Substring_index (SUBSTRING (commentid,9), '-', 1), '-', -1) from check WHERE Commentid = ' content_13 -11220-1 '

We find that there is a need to call three times function, there is no can be called two times. So we can write this:
Copy Code code as follows:
SELECT Substring_index (Substring_index (Commentid, '-', 1), ' _ ', -1) from check WHERE Commentid = ' content_13-11220-1 '

This allows for less execution of the function, and when we run enough data, the speed is obvious.

MySQL more functions can refer to MySQL function encyclopedia: http://www.jb51.net/article/42906.htm

More about MySQL interested readers can view the site topics: "MySQL common functions large summary", "MySQL Log Operation skills Encyclopedia", "MySQL Transaction operation skills Summary", "MySQL stored process skills encyclopedia" and "MySQL database lock related skills summary"

I hope this article will help you with the MySQL database meter.

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.