Detailed explanation of how to query fields containing '%' in MySQL like (ESCAPE usage)

Detailed explanation of how to query fields containing '%' in MySQL like (ESCAPE usage)

In the SQL like statement, for example

SELECT * FROM user WHERE username LIKE '%luchi%'
SELECT * FROM user WHERE username LIKE '_luchi_',
  • % as a wildcard to match multiple
  • _ as a wildcard

But when the field to be queried by like contains %, how do we check:

At this time, you need to specify that the '%' in the field is not used as a wildcard;
Here we need to use ESCAPE escape

test:

Here we use this table

lc

Before escaping:

SELECT * FROM user WHERE username LIKE '%%%'; 


Here you will find that the three % signs are all treated as wildcards;

After escaping:

SELECT * FROM user WHERE username LIKE '%1%%' ESCAPE '1'; 


Query successful

Note:

  • ESCAPE is followed by a character, and what is written inside is the escape character;
  • Then just like the escape characters in C language, such as '\n', '\t', write this character before the % sign you need to escape;

Tips and Suggestions:

MySQL wildcards are very useful. This functionality comes at a cost, however: wildcard searches generally take longer to process than the other searches discussed previously. Here are some tips to remember when using wildcards.

  • Don't overuse wildcards. If other operators can achieve the same purpose, you should use other operators.
  • When you do need to use wildcards, don't use them at the beginning of a search pattern unless absolutely necessary. Placing the wildcard at the beginning of the search pattern is the slowest search.
  • Note carefully the placement of the wildcard characters. If placed in the wrong place, it may not return the expected number.

This is the end of this article on how to use MySQL like to query fields containing '%' (ESCAPE usage). For more information about MySQL like 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:
  • Detailed explanation of the usage of the ESCAPE keyword in MySQL
  • Analysis of mysql_escape_string() Function Usage
  • Detailed use cases of MySql escape

<<:  Native JS to achieve blinds special effects

>>:  Solution to Nginx SSL certificate configuration error

Recommend

How to use crontab to backup MySQL database regularly in Linux system

Use the system crontab to execute backup files re...

Docker deploys nginx and mounts folders and file operations

During this period of time, I was studying docker...

Detailed description of mysql replace into usage

The replace statement is generally similar to ins...

How to click on the a tag to pop up the input file upload dialog box

html Copy code The code is as follows: <SPAN cl...

Docker container orchestration implementation process analysis

In actual development or production environments,...

WHMCS V7.4.2 Graphical Installation Tutorial

1. Introduction WHMCS provides an all-in-one solu...

The solution of html2canvas that pictures cannot be captured normally

question First, let me talk about the problem I e...

js to achieve 3D carousel effect

This article shares the specific code for impleme...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

Detailed explanation of React setState data update mechanism

Table of contents Why use setState Usage of setSt...

How to disable the automatic password saving prompt function of Chrome browser

Note: In web development, after adding autocomplet...

A brief discussion on tags in HTML

0. What is a tag? XML/HTML CodeCopy content to cl...

Layim in javascript to find friends and groups

Currently, layui officials have not provided the ...

Why web page encoding uses utf-8 instead of gbk or gb2312?

If you have a choice, you should use UTF-8 In fac...