Mysql (6) string pattern matching _ MySQL

Source: Internet
Author: User
Mysql (6) string pattern matching bitsCN.com


Mysql (6) string pattern matching

Related links:

Mysql (1) installation of mysql

Http: // database/201210/162314 .html;

Mysql-related operations (2)

Http: // database/201210/162315 .html;

Mysql operations on data tables (3)

Http: // database/201210/162316 .html;

Mysql (4) Data Table query operations

Http: // database/201210/162317 .html;

Mysql operations (5)

Http: // database/201210/162318 .html

I. matching the standard SQL Mode

In the matching mode of mysq, you can use "_" to match any single character and "%" to match any number of characters.

= OR! =, Use the LIKE or not like comparison operator.

Example:

1. find the name starting with "B" in the student table.

SELECT * FROM student WHERE name LIKE "B % ";

2. find the name ending with "B" in the student table.

SELECT * FROM student WHERE name LIKE "% B ";

3. find the name "B" in the student table.

SELECT * FROM student WHERE name LIKE "% B % ";

4. find the name exactly five characters in the student table.

SELECT * FROM student WHERE name LIKE "_"; (there are spaces to indicate that there are 5 "_")

II. Extended Regular expression pattern matching

When using regular expressions, you need to use other comparison operators, which are REGEXP or not regexp.

Extended Regular expression characters:

"." Matches any single character.

"*" Matches zero or multiple characters before it.

"X *" matches any number of X characters.

Regular expressions are case-sensitive and can be matched using a character class. [AA] matches lowercase and upper-case.

"^" Indicates the beginning and "$" indicates the end.

Example:

1. find the name starting with "B" in the student table.

SELECT * FROM student WHERE name REGEXP "^ [bB]";

2. find the name ending with "B" in the student table.

SELECT * FROM student WHERE name REGEXP "[bB] $ ";

3. find the name "B" in the student table.

SELECT * FROM student WHERE name REGEXP "[bB]";

4. find the name exactly five characters in the student table.

SELECT * FROM student WHERE name REGEXP "^ _ $"; (there are spaces to indicate that there are 5 "_")

BitsCN.com

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.