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

Explaining immutable values ​​in React

Table of contents What are immutable values? Why ...

React.js framework Redux basic case detailed explanation

react.js framework Redux https://github.com/react...

Sample code for implementing dark mode with CSS variables

Recently, WeChat was forced by Apple to develop a...

Analysis of MySQL example DTID master-slave principle

Table of contents 1. Basic Concepts of GTID 2. GT...

Detailed explanation of two ways to dynamically change CSS styles in react

The first method: dynamically add a class to show...

Docker installs redis 5.0.7 and mounts external configuration and data issues

Redis is an open source NoSQL database written in...

How to view files in Docker image

How to view files in a docker image 1. If it is a...

Explanation of MySQL performance inspection through show processlist command

The show processlist command is very useful. Some...

Detailed examples of Linux disk device and LVM management commands

Preface In the Linux operating system, device fil...

The difference between html, xhtml and xml

Development Trends: html (Hypertext Markup Languag...

Display mode of elements in CSS

In CSS, element tags are divided into two categor...

An example of how JavaScript can prevent duplicate network requests

Preface During development, we often encounter va...