MySQL Fuzzy query like/regexp

Source: Internet
Author: User
Tags ming

Adding and Deleting is the most basic function of MySQL, which is the most frequent operation, Fuzzy Lookup is very common in the query operation, so fuzzy search has become a compulsory course.

Like mode

Like means look, there are two modes: _ and%

_ denotes a single character, usually used to query a fixed length of data, such as the names of all the three characters of the king, assuming the name column named name, note that "Wang" followed by two _

Select name from table name where name like ' King __ ';

% represents 0 or more arbitrary characters, such as the name of all Wang

Select name from table name where name like ' King% ';

Find out all the names that contain the word "Hua"

Select name from table name where name like '% Hua ';

Regular Mode

^, match the starting position of the string, or the above example, query all the names of the king

Select name from table name where name regexp ' ^ King ';

$, matches the end of the string, such as querying all names at the end of the name "Ming"

Select name from table name where name RegExp ' Ming $ ';

., match any single character except \ n, like _, without writing the SQL statement

[...], matching any one of the characters contained in [], ABCDEF......XYZ can be abbreviated as [a-z],0123456789] [0-9], such as the name of the person who w/z/s the beginning of the query.

Select name from table name where name RegExp ' ^[wzs] ';

[^ ...], matching characters not included in [], such as querying for names other than the beginning of w/z/s

Select name from table name where name RegExp ' ^[^wzs] ';

A|b|c, match A or B or C, if the employee who performance is a-or a or a + is identified, assuming the performance column name performance

Select performance from table name where performance RegExp ' a-| a| A + ';

*, repeat 0 or more times, familiar with JavaScript regular students know

' str* ' can match st/str/strr/strrr ...


?, repeat 0 or 1 times

' Str? ' Can match St/str


+, repeat 1 or more times

' str+ ' can match str/strr/strrr/strrrr ...

Compared to the regular in JavaScript, here the regular is a simplified version, there is no lazy match/greedy match, [] \w\s\d this syntax is not supported, also does not support Chinese, relatively simple.

one point to note is that the two modes do not mix, like mode is not supported regular expression, RegExp mode does not know _ and%

MySQL Fuzzy query like/regexp

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.