MYSQL fuzzy Query, scope query

Source: Internet
Author: User

Example: SQL fuzzy query, using the like comparison keyword, plus the wildcard characters in SQL, please refer to the following:1, like' mc% 'All strings (such as McBadden) that begin with the letter Mc will be searched.2, like'%iNger 'All strings ending with the letter inger (such as Ringer, Stringer) will be searched.3, like'%en% 'All strings (such as Bennet, Green, McBadden) that contain the letter en are searched at any location.4, like' _heryl 'Searches for all six-letter names (such as Cheryl, Sheryl) that end with the letter heryl.5, like' [ck]ars[eo]n 'The following strings are searched: Carsen, Karsen, Carson, and Karson (such as Carson).6, like' [M-z]inger 'Searches for all names (such as Ringer) that end with a string inger, starting with any single letter from M to Z.7, like' m[^c]% 'The search begins with the letter M, and the second letter is not all names of C (such as Macfeather). Theory: SELECT field from table where a field like condition where about the condition, SQL provides four matching modes:1,%: Represents any0One or more characters. Can match any type and length of characters, in some cases, if Chinese, use two percent sign (%%Said Like SELECT*From [user] WHERE u_name like'% three 'will be u_name for "Zhang San", "Zhang Cat Three", "three-legged Cat", "Tang Sanzang" and so on Have "three" records all find out. Also, if you need to find a record of "three" and "cat" in U_name, use the and condition select*From [user] WHERE u_name like'% three 'and u_name like'% cat% 'If you use SELECT*From [user] WHERE u_name like'% of cat% 'Although can search out "three feet cat", but can not search out the eligible "Zhang Cat three".2, _: Represents any single character. Matches a single arbitrary character, which is commonly used to restrict the expression's character-length statements: for example, SELECT*From [user] WHERE u_name like' _ three _ 'Only find "Tang Sanzang" so u_name for three words and one word in the middle is "three"; another example of select*From [user] WHERE u_name like' three __ ';Just find out "three-legged cat" this name is three words and the first word is "three";3, []: represents one of the characters listed in parentheses (similar to a regular expression). Specifies a character, string, or range that requires matching objects to be any of them. Like SELECT*From [user] WHERE u_name like' [Zhang Li Wang] three '"Zhang San", "Lie Triple", "Wang San" (rather than "Zhangli Kang") will be found, as in [] a series of characters (01234, ABCDE, etc.) can be written as "0-4"," a-E "Select*From [user] WHERE u_name like' old [1-9] 'Will find out "old1"," the old2"、......、" old9”;4,[^]: Represents a single character that is not listed in parentheses. The value is the same as [], but it requires that the matched object be any character other than the specified character. Like SELECT*From [user] WHERE u_name like' [^ Zhang Li Wang] three 'Will find the surname "Zhang", "Li", "Wang" "Zhao Three", "Magozo" and so on; select*From [user] WHERE u_name like' old [^1-4] ';Will exclude "old1"To" the old4", looking for" the old5"," the old6”、......5, the query content contains wildcards because of the wildcard character, which causes us to query special characters "%"," _ "," ["statements are not implemented properly, and special characters with" [] "can be queried normally. Thus we write the following functions: function Sqlencode (Str)Str=ReplaceStr,"[","[[]")' This sentence must be in the topstr=replace (str, "_", "[_]")str=replace (str, "%", "[%]")Sqlencode=strEnd FunctionThe unknown origin string is processed by the function before the query, and it is important to note that when a database is connected to a Web page using this type of query statement:such as SELECT * from the user Where name like 'Old^1-4]" above" 'Old^1-4]《' "is to have single quotation marks, do not forget. # #范围查询query a specified range, such as a table with an ID, to query, id=1,2,3:SELECT *from table_name where ID in (n/a);queries in a range, such as the query ID in 2~8:SELECT *from table_name where ID between 2 and 8;between can connect multiple and, that is, multiple criteria can be addedAnd is higher than or, and if it appears at the same time and wants to first operate or, you can use () parentheses. 

MYSQL fuzzy Query, scope query

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.