MySQL string truncation and splitting function usage example

Source: Internet
Author: User

MySQL string truncation and splitting function usage example

This example describes how to use the string functions of MySQL to intercept and split. We will share this with you for your reference. The details are as follows:

FirstString truncation function:

SUBSTRING(commentid,9)

This is very simple, starting from 9th characters to the end. There are three SUBSTRING parameters. The last one is the truncation length. The default value is to the end, and the negative value is the last digit.

NextSplit string functions:

SUBSTRING_INDEX(commentid, '-', 1)

This is a little more complicated. It means to split the string and retrieve all the strings from the first keyword. If the third parameter is changed to-1, the last parameter is used. What if we want to extract 50 or 11065 from the string c-11065-50?

Obtain 50 statements:
Copy codeThe Code is as follows: SELECT SUBSTRING_INDEX (checkid, '-',-1) FROM 'check' WHERE checkid = 'C-11065-50'
Write 11065 of the results:
Copy codeThe Code is as follows: SELECT SUBSTRING_INDEX (checkid, '-',-2), '-', 1) FROM check WHERE checkid = 'C-11065-50'
Or:
Copy codeThe Code is as follows: SELECT SUBSTRING_INDEX (checkid, '-', 2), '-',-1) FROM check WHERE checkid = 'C-11065-50'
It looks complicated, so it's more complicated:

This below is the combination of usage, for example we want to intercept: 13 in the content_13-11220-1, the simplest is:
Copy codeThe Code is as follows: SELECT SUBSTRING_INDEX (SUBSTRING (commentid, 9), '-', 1), '-',-1) FROM check WHERE commentid = 'content _ 13-11220-1'
We found that three functions need to be called here. Can we call them twice. So we can write:
Copy codeThe Code is as follows: SELECT SUBSTRING_INDEX (commentid, '-', 1), '_',-1) FROM check WHERE commentid = 'content _ 13-11220-1'
In this way, the Merge function can be executed less. When we run enough data, the speed will be obvious.

MySQL more functions can refer to MySQL functions Daquan: http://www.bkjia.com/article/42906.htm

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.