MySQL pattern matching two methods

Source: Internet
Author: User

    • I. Using a like or not-like comparison operator

Use "_" to match any single character, while "%" matches any number of characters (including 0 characters);

For example:

1. To find out which name begins with "B":

MySQL>SELECT*fromWHEREto like'b% ';


2. To find out “fy” the name with the end:
MySQL>SELECT*fromWHEREto like'%fy' ;
3. To find out “w” which name to include:
MySQL>SELECT*fromWHERElike' %w% ';

4. To find a name that contains exactly 5 characters, use the “_” pattern character:

MySQL>SELECT*fromWHEREto like'_____ ';

    • Second, using the regexp and not regexp operators

⑴ "." matches any single character;

The ⑵ character class “[...]” matches any character within the square brackets. For example, “[abc]” match “a” , “b” or “c” . To name a range of characters, use a "-". “[a-z]”matches any letter, and “[0-9]” matches any number.

⑶ “ * ”Match 0 or more characters in front of it. For example, match any number of “x*” “x” characters, match any number of “[0-9]*” numbers, and “.*” match any number of characters.

⑷ to locate a pattern so that it must match the beginning or end of the value being tested, or at the beginning of the pattern “^” 在模式的结尾用“$” .

1. In order to find the first “b” name, use the beginning of the “^” matching name:

MySQL>SELECT*fromWHERE'^b';

2. If you want to force regexp to be case-sensitive, use the binary keyword to make one of the strings into a binary string. The query matches only the lowercase ' b ' of the first letter of the name:

MySQL>SELECT*fromWHEREBINARY'^b  ';

3. In order to find with the “fy” end of the name, use the end of the “$” matching name:

MySQL>SELECT*fromWHERE'fy$ ';

4. To find “w” the name that contains one, use the following query:

MySQL>SELECT*fromWHERE'w';

5. In order to find the name containing exactly 5 characters, use “^” and “$” match the start and end of the name, and 5 “.” instances between the two:

MySQL>SELECT*fromWHERE'^.....$ ';

You can also “{n}” rewrite the previous query using the repeat N times operator:

MySQL>SELECT*fromWHERE'^.{ 5}$';

    • Difference
    1. like matches the value of the entire field, for example, do not use "_" or "%" words, use the above SQL query contains a "w"  name, such as "w" is not found, and regexp can;
    2. Like can only use "_" and "%", and regular expressions are not supported.

MySQL pattern matching two methods

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.