Use wildcards to filter-like

table of Contents

Like operator: select column name from table name where column name like'A%';

Percent sign (%) wildcard

Underscore (_) wildcard

Tips for using wildcards


Like operator: select column name from table name where column name like'A%';

Wildcard: A special character used to match part of a value.

Percent sign (%) wildcard

% Tells MySQL to accept any character after A, no matter how many characters it has.

Wildcards can be used anywhere in the search pattern, and multiple wildcards can be used .

Wildcards can also appear in the middle of the search pattern.

In addition to one or more characters,% can also match 0 characters. Although it seems that the% wildcard can match anything, there is one exception, which is NULL. Even WHERE prod_name LIKE'%' cannot match rows with the value NULL as the product name.

Underscore (_) wildcard

The underscore matches only a single character and not multiple characters.

If you use% to match, another result appears: _ always matches one character, and% can match multiple characters

Unlike %, which can match 0 characters, _ always matches one character, no more or no less.

Tips for using wildcards

1. Don't overuse wildcards. If other operators can achieve the same purpose, other operators should be used. The processing of wildcard searches generally takes longer than the other searches discussed earlier.
2. When you really need to use wildcards, don't use them at the beginning of the search pattern unless absolutely necessary. Put the wildcard at the beginning of the search pattern, the search is the slowest.
3. Pay careful attention to the position of wildcards. If it is misplaced, the desired data may not be returned.

Guess you like

Origin blog.csdn.net/weixin_49984044/article/details/108664789