Four matching modes for SQL

Source: Internet
Author: User

1.% denotes any 0 or more characters. The following statement: Select * from the user Where name like '% three '; will be the name of "Zhang San", "three-legged Cat", "Tang Sanzang" and so on have "three" all find out. % Three: Indicates a left match. Three: Indicates a right match. % Three: Indicates a fuzzy query. 2. _ denotes any single character. Statement: SELECT * from the user Where name like ' _ three _ '; just find "Don Sanzang". This name is three characters and the middle word is "three"; Select * from user Where name is like ' three __ '; only find "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 regular expressions). Statement: Select * from user Where name is like ' [Zhang Li Wang] three '; "Zhang San", "Lie Triple", "Wang San" (rather than "Zhangli Kang") will be found, and a series of characters (01234, ABCDE, etc.) within [] may be written as "0-4", "a-e". SELECT * from user where name is like ' old [1-9] '; will find "Old 1", "Old 2" 、......、 "Old 9"; If you want to find the "-" character, put it in the first place: ' Zhang San [-1-9] '; 4. [^] represents a single character that is not listed in parentheses. Statement: SELECT * from the user where name like ' [^ Zhang Li Wang] three '; will find the surname "Zhang", "Li", "King" of the "Zhao three", "Magozo" and so on; Select * from user where name as ' old [^1-4] '; Will exclude "old 1" to "Old 4" Looking for "old 5", "Old 6" 、......、 "Old 9". 5.* means that all information is found, such as SELECT * from Tbl_user and finally the point! As a result of the wildcard, we query the special characters "%", "_", "[", "'"; Statements are not implemented properly, and the special characters are queried normally by "[]". Accordingly we write the following functions: function Sqlencode (str) str=replace (str, "';", "'; ';") Str=replace (str, "[", "[[]") '; This sentence must be in the first str=replace (str, "_", "[_]") Str=replace (str, "%", "[%]") sqlencode=str enD function before querying the unknown origin string is processed by the function, and on the Web page to connect the database to use this kind of query statement to pay attention to: such as SELECT * from the user Where name like ' old [^1-4] ', above the "old [^1-4]" " is to have single quotation marks, do not forget, I often forget!

Four matching modes for SQL

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.