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

The method of querying fields with mysql wildcard

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the method of mysql wildcard query field, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Mysql wildcard query field method: 1, use [%] to represent any character any number of times, code is [WHERE title like'% Zhang San']; 2, use [_] to represent a single character, code is [WHERE title like'_ Zhang San'].

How to query fields with mysql wildcards:

First of all, let's take a look at two concepts, one is an operator and the other is a wildcard.

Operator

Like is the operator in the SQL statement, and its function is to indicate that the search pattern after the SQL statement is compared using wildcards rather than direct equality matching.

Note: if you do not use wildcards when using the like operator, the effect is the same as the equal sign.

SELECT id,title FROM table WHERE title like 'Zhang San'

This way of writing can only match Zhang San's records, not records like Zhang San is a good man.

Wildcard character

% (percent sign), _ (underscore) are wildcards,% means any character appears any number of times (can be 0 times), and _ represents a single character, users are as follows:

SELECT id,title FROM table WHERE title like 'Zhang San%'; SELECT id,title FROM table WHERE title like'% Zhang San'; SELECT id,title FROM table WHERE title like'% Zhang San%'

Indicates matching records that begin with Zhang San, 2 indicates matching records that end with Zhang San, and 3 indicates matching records that contain Zhang San.

SELECT id,title FROM table WHERE title like 'Zhang San'; SELECT id,title FROM table WHERE title like'_ Zhang San'

It means to match records like Zhang Sanhao, and 2 means to match records like Hello Zhang San.

Attention to use

Pay attention to case. When using fuzzy matching, that is, when matching text, mysql may or may not be case-sensitive, depending on how the user configures MySQL.

Pay attention to the trailing space.

Note that NULL,% can match any character, but not NULL.

Reasonable use

MySQL's wildcards are useful, but this feature comes at a cost. Wildcard searches generally take longer than other searches discussed earlier, and here are some tips to keep in mind when using wildcards.

Don't overuse wildcards. If other operators can achieve the same goal, you should use other operators.

When you do need to use wildcards, do not use them at the beginning of the search pattern unless absolutely necessary. Put wildcards at the beginning of the search pattern, which is the slowest to search.

Pay close attention to the position of the wildcard, if misplaced, it may not return the desired number.

The above is all the contents of the mysql wildcard query field method, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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