A brief discussion on the implementation of fuzzy query using wildcards in MySQL

A brief discussion on the implementation of fuzzy query using wildcards in MySQL

In the MySQL database, when we need fuzzy query, we will use wildcards.

First, let's understand two concepts, one is the operator and the other is the wildcard.

Operators

Like is an operator in SQL statements. Its function is to indicate that the search pattern following the SQL statement is to be compared using wildcards instead of direct equality matching.

Note: If you use the like operator without wildcards, the effect is the same as the equal sign.

SELECT id,title FROM table WHERE title like '张三';

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

Wildcards

% (percent sign) and _ (underscore) are wildcards. % means any character can appear any number of times (can be 0 times), and _ means a single character. The user is as follows:

1 SELECT id,title FROM table WHERE title like '张三%';
2 SELECT id,title FROM table WHERE title like '%张三';
3 SELECT id,title FROM table WHERE title like '%张三%';

1 means matching records that start with Zhang San, 2 means matching records that end with Zhang San, and 3 means matching records that contain Zhang San.

1 SELECT id,title FROM table WHERE title like '张三_';
2 SELECT id,title FROM table WHERE title like '__张三';

1 means matching records like Hello Zhangsan, and 2 means matching records like Hello Zhangsan.

Usage Notes

Pay attention to case. When using fuzzy matching, that is, matching text, MySQL may be case-sensitive or insensitive. The result depends on how the user configures MySQL.

Note the trailing space.

Note that NULL and % can match any character, but cannot match NULL.

Fair Use

MySQL wildcards are very useful, but this functionality comes at a price. Wildcard searches generally take longer to process than the other searches discussed earlier. Here are some tips to remember when using wildcards.

Do not overuse wildcards; use other operators if they can achieve the same purpose.

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

Pay careful attention to the placement of the wildcard characters; if they are misplaced, the expected number may not be returned.

This is the end of this article about MySQL wildcard fuzzy query. For more information about MySQL wildcard fuzzy query, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL fuzzy query usage (regular, wildcard, built-in function)
  • Summary of MySQL database like statement wildcard fuzzy query
  • Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)
  • A brief discussion on wildcard escape in MySQL fuzzy query
  • MYSQL Must-know Reading Notes Chapter 8: Using Wildcards for Filtering
  • Things to note when using wildcards in MySQL
  • Detailed introduction to the use of MySql like fuzzy query wildcards
  • mysql regular expression LIKE wildcard
  • mysql wildcard (sql advanced filtering)

<<:  Tomcat9 download, installation and configuration + detailed tutorial on integrating into eclipse

>>:  JS realizes video barrage effect

Recommend

How to use MySQL stress testing tools

1. MySQL's own stress testing tool - Mysqlsla...

How to use the concat function in mysql

As shown below: //Query the year and month of the...

Detailed description of HTML table border control

Only show the top border <table frame=above>...

Use CSS to achieve circular wave effect

I often see some circular wave graphics on mobile...

Detailed explanation of JavaScript function introduction

Table of contents Function Introduction function ...

Vue implements form data validation example code

Add rules to the el-form form: Define rules in da...

Regular expression usage in CSS selectors

Yes, CSS has regular expressions too (Amen) Two p...

Solution to mysql failure to start due to insufficient disk space in ubuntu

Preface Recently, I added two fields to a table i...

Detailed explanation of the use of this.$set in Vue

Table of contents Use of this.$set in Vue use Why...

How to implement checkbox & radio alignment

Not only do different browsers behave differently...

XHTML Tutorial: XHTML Basics for Beginners

<br />This site’s original content, please i...

MySQL Series 3 Basics

Table of contents Tutorial Series 1. Introduction...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux seq command can generate lists of numbe...