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

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows: Dynami...

How to delete the container created in Docker

How to delete the container created in Docker 1. ...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...

How to deploy Angular project using Docker

There are two ways to deploy Angular projects wit...

Tutorial on using Docker Compose to build Confluence

This article uses the "Attribution 4.0 Inter...

Installation and use of mysql on Ubuntu (general version)

Regardless of which version of Ubuntu, installing...

Solve the problem of insufficient docker disk space

After the server where Docker is located has been...

ElementUI component el-dropdown (pitfall)

Select and change: click to display the current v...

Automated front-end deployment based on Docker, Nginx and Jenkins

Table of contents Preliminary preparation Deploym...

Some key points of website visual design

From handicraft design to graphic design to web de...

How to write transparent CSS for images using filters

How to write transparent CSS for images using filt...