Detailed explanation of the usage of the ESCAPE keyword in MySQL

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape

Escape means the original semantics of the escape character. The purpose of an escape character is to start a character sequence so that the character sequence starting with the escape character has different semantics than when the character sequence appears alone.

In MySQL, the escape character starts with "\". The escape characters commonly used in programming are all valid in MySQL and will not be described or discussed here. Here, the role of the ESCAPE keyword is mainly explained through "%" and "_".

%: Matches any number of characters.

_: Matches a single character.

If we want to match "%" or "_", we must use "\" to escape, as follows:

### Query users whose names contain the character "明"> SELECT * FROM user WHERE name LIKE CONCAT("%", "明", "%")
 
### Query users whose names contain the % character> SELECT * FROM user WHERE name LIKE CONCAT("%", "\%", "%")

Usage of ESCAPE

The main function of the ESCAPE keyword is to specify a character to replace the "\".

### Query users whose names contain the "%" character> SELECT * FROM user WHERE name LIKE CONCAT("%", "$%", "%") ESCAPE "$"
 
### Query users whose names contain the "_" character> SELECT * FROM user WHERE name LIKE CONCAT("%", "a_", "%") ESCAPE "a"

It should be noted that all characters indicated by ESCAPE in the query conditions will replace the function of "\".

### Assume there are two users named %a and %_> SELECT * FROM user WHERE name LIKE "a%_" ESCAPE "a" ### %a %_ 
> SELECT * FROM user WHERE name LIKE "a%a" ESCAPE "a" ### %a 
> SELECT * FROM user WHERE name LIKE "a%a_" ESCAPE "a" ### %_

This is the end of this article on the detailed usage of the ESCAPE keyword in MySQL. For more relevant MySQL ESCAPE keyword content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of how to query fields containing '%' in MySQL like (ESCAPE usage)
  • Analysis of mysql_escape_string() Function Usage
  • Detailed use cases of MySql escape

<<:  Detailed explanation of the implementation process of Nginx enabling Brotli compression algorithm

>>:  HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

Recommend

Introduction to TypeScript basic types

Table of contents 1. Basic types 2. Object Type 2...

VMWare Linux MySQL 5.7.13 installation and configuration tutorial

This article shares with you the tutorial of inst...

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small func...

MySql grouping and randomly getting one piece of data from each group

Idea: Just sort randomly first and then group. 1....

Implementation of CSS sticky footer classic layout

What is a sticky footer layout? Our common web pa...

Detailed explanation of TypeScript 2.0 marked union types

Table of contents Constructing payment methods us...

svg+css or js to create tick animation effect

Previously, my boss asked me to make a program th...

VUE implements a Flappy Bird game sample code

Flappy Bird is a very simple little game that eve...

7 interesting ways to achieve hidden elements in CSS

Preface The similarities and differences between ...

MySQL log settings and viewing methods

MySQL has the following logs: Error log: -log-err...

Solve the splicing problem of deleting conditions in myBatis

I just learned mybatis today and did some simple ...

Native js implements custom scroll bar component

This article example shares the specific code of ...