MySQL string intercept function substring usage description

Source: Internet
Author: User
Tags php explode truncated

Function:

1. Intercept the string from left (str, length)
Description: Left (truncated field, intercept length) Example: Select Left (content,200) as abstract from my_content_t

2. Intercept string Right (str, length)
Description: Right (truncated field, intercept length) Example: Select Right (content,200) as abstract from my_content_t

3. Intercept string substring (str, POS)
SUBSTRING (str, pos, Length) Description: substring (intercepted field, intercepted from first)
substring (truncated field, intercept from first, intercept length) Example: Select substring (content,5) as abstract from my_content_t Select substring (content, 5,200) as abstract from my_content_t (note: If the number of digits is negative such as-5 is the number of digits from the back, to the end of the string or the length of the Intercept)
4, by keyword intercept string substring_index (Str,delim,count)
Description: Substring_index (the number of occurrences of the intercepted field, keywords, keywords) example: Select Substring_index ("Blog.jb51.net", ". ", 2) as abstract from my_content_t
Results: blog.jb51 (Note: If the number of occurrences of a keyword is negative, 2 is the inverse from the back to the end of the string)

Introduction to Functions:

SUBSTRING (str,pos), SUBSTRING (str from pos) SUBSTRING (str,POS ,len), SUBSTRING (str from pos for len)

The format without the len parameter returns a substring from the string str , starting at position Pos. The format with the len parameter returns a substring of the same length as the Len character from the string str , starting at position Pos. Use the from format as standard SQL syntax. You may also use a negative value for the POS . If so, the position of the substring starts at the POS character at the end of the string, not at the beginning of the string. You can use a negative value for the POS in the following format function.

Please refer to the manual for details.

Example: Table 1:user

Table 2:JL

Desired effect: Through the ID value stored in the User table Jlid field, read the corresponding record in the JL table, here want to read, in the JL table in the ID 1, 2 records, first thought with in, but unfortunately because the jlid field stored in the ID value of 2, although formally in accordance with the format, But if you use select jl.* from the JL where Jl.id in (select Jlid from user where user.id=1) to query, it is not possible, he always returns the record with ID 1.

So what do we do? If we were able to get 1 and 2 respectively, we could. Fortunately, MySQL also provides string intercept function substring.

The SQL syntax is as follows: SELECT JL. * from JL WHERE jl.id = (SELECT SUBSTRING (

SELECT user.jlid from user WHERE user.id =1), 1, 1) OR jl.id = (

SELECT SUBSTRING ((

SELECT user.jlid from user WHERE user.id =1), 3, 1)) LIMIT 0, 30

Simply explain:

Select SUBSTRING ((select user.jlid from user WHERE user.id =1), 1, 1) This uses a subquery, first querying the value of the Jlid field in the user table with ID 1, returning a string, and then using the Sub The string is intercepted to get the strings 1 SELECT SUBSTRING ((select user.jlid from user WHERE user.id =1), 3, 1)) This statement gets 2

1 and 2 have been again through the main query where to query, to note that we need to query id=1 and id=2 records, so the use of or, how, is not a little trouble,

Your first instinct is not to use 2 SQL statements, in the middle with PHP explode function to query it? This is normal, but the two between who's high efficiency, stationmaster did not test, hope that the heart of you, can help Oh!

Http://www.jb51.net/article/27458.htm

Http://www.jb51.net/article/39199.htm

MySQL string intercept function substring usage description

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.