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

Detailed explanation of how to use the Vue license plate search component

A simple license plate input component (vue) for ...

MySQL Series 11 Logging

Tutorial Series MySQL series: Basic concepts of M...

Implementation of vscode custom vue template

Use the vscode editor to create a vue template, s...

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

Table of contents Preliminary preparation Deploym...

Ubuntu16.04 builds php5.6 web server environment

Ubuntu 16.04 installs the PHP7.0 environment by d...

How to modify iTunes backup path under Windows

0. Preparation: • Close iTunes • Kill the service...

The concept of MySQL tablespace fragmentation and solutions to related problems

Table of contents background What is tablespace f...

15 Vim quick reference tables to help you increase your efficiency by N times

I started using Linux for development and enterta...

5 Tips for Protecting Your MySQL Data Warehouse

Aggregating data from various sources allows the ...

JavaScript macrotasks and microtasks

Macrotasks and Microtasks JavaScript is a single-...

Web lesson plans, lesson plans for beginners

Teaching Topics Web page Applicable grade Second ...