Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use wildcards in MySQL

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to use wildcards in MySQL. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

MySQL wildcard

SQL pattern matching allows you to use "_" to match any single character, while "%" matches any number of characters (including zero characters). In MySQL, the mode of SQL ignores case by default. Some examples are shown below.

Note that when you use SQL mode, you cannot use = or! =; instead, use the LIKE or NOT LIKE comparison operator.

To find out the name that starts with "b":

Mysql > SELECT * FROM pet WHERE name LIKE "b"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

| | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | |

+-+ +

To find out the name that ends with "fy":

Mysql > SELECT * FROM pet WHERE name LIKE "fy"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Fluffy | Harold | cat | f | 1993-02-04 | NULL |

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

+-+ +

To find a name that contains a "w":

Mysql > SELECT * FROM pet WHERE name LIKE "w"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Claws | Gwen | cat | m | 1994-03-17 | NULL |

| | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | |

| | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |

+-+ +

To find a name that contains exactly 5 characters, use the "_" mode character:

Mysql > SELECT * FROM pet WHERE name LIKE "_"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Claws | Gwen | cat | m | 1994-03-17 | NULL |

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

+-+ +

Other types of pattern matching provided by MySQL are using extended regular expressions. When you do matching tests on such patterns, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT RLIKE, which are synonyms).

Some of the characters that extend regular expressions are:

"." Matches any single character.

A character class "[.]" Matches any character in square brackets. For example, "[abc]" matches "a", "b", or "c". To name a range of characters, use a "-". "[amurz]" matches any lowercase letter, while "[0-9]" matches any number.

"*" matches zero or more things in front of it. For example, "x*" matches any number of "x" characters, "[0-9] *" matches any number of numbers, and ". *" matches any number of anything.

Regular expressions are case-sensitive, but if you want, you can use one character class to match both. For example, "[aA]" matches lowercase or uppercase "a" and "[a-zA-Z]" matches any of the two letters.

If it appears anywhere in the value being tested, the pattern matches (as long as they match the entire value, the SQL pattern matches).

To locate a pattern so that it must match the beginning or end of the value being tested, use "^" at the beginning of the pattern or "$" at the end of the pattern.

To illustrate how extended regular expressions work, the LIKE query shown above is rewritten using REGEXP below:

To find names that start with "b", use "^" to match the beginning of the name and "[bB]" to match lowercase or uppercase "b":

Mysql > SELECT * FROM pet WHERE name REGEXP "^ [bB]"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

| | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | |

+-+ +

To find the name that ends with "fy", use "$" to match the end of the name:

Mysql > SELECT * FROM pet WHERE name REGEXP "fy$"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Fluffy | Harold | cat | f | 1993-02-04 | NULL |

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

+-+ +

To find a name that contains a "w", use "[wW]" to match a lowercase or uppercase "w":

Mysql > SELECT * FROM pet WHERE name REGEXP "[wW]"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Claws | Gwen | cat | m | 1994-03-17 | NULL |

| | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | |

| | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |

+-+ +

Since a regular expression appears anywhere in the value and its pattern matches, it is no longer necessary to place a wildcard on both sides of the pattern in the previous query to make it match the entire value, just as if you used an SQL schema.

To find a name that contains exactly five characters, use "^" and "$" to match the beginning and end of the name, and 5 "." The instance is between the two:

Mysql > SELECT * FROM pet WHERE name REGEXP "^. $"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Claws | Gwen | cat | m | 1994-03-17 | NULL |

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

+-+ +

You can also rewrite the previous query using the "{n}"repeat n times" operator:

Mysql > SELECT * FROM pet WHERE name REGEXP "^. {5} $"

+-+ +

| | name | owner | species | sex | birth | death | |

+-+ +

| | Claws | Gwen | cat | m | 1994-03-17 | NULL |

| | Buffy | Harold | dog | f | 1989-05-13 | NULL |

+-+ +

On how to use wildcards in MySQL to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report