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

Linux uses iftop to monitor network card traffic in real time

Linux uses iftop to monitor the traffic of the ne...

Solve the problem of ugly blue border after adding hyperlink to html image img

HTML img produces an ugly blue border after addin...

Mini Program to Implement Calculator Function

This article example shares the specific code of ...

Detailed explanation of MySQL Strict Mode knowledge points

I. Strict Mode Explanation According to the restr...

How to implement second-level scheduled tasks with Linux Crontab Shell script

1. Write Shell script crontab.sh #!/bin/bash step...

The difference between html empty link href="#" and href="javascript:void(0)"

# contains a location information. The default anc...

Centos builds chrony time synchronization server process diagram

My environment: 3 centos7.5 1804 master 192.168.1...

Use of Linux usermod command

1. Command Introduction The usermod (user modify)...

A brief analysis of CSS :is() and :where() coming to browsers soon

Preview versions of Safari (Technology Preview 10...

Details about the like operator in MySQL

1. Introduction When filtering unknown or partial...

VMware Workstation 14 Pro installation Ubuntu 16.04 tutorial

This article records the specific method of insta...

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

Detailed explanation of several ways to create objects and object methods in js

This article is the second article about objects ...

MySQL permission control details analysis

Table of contents 1. Global level 2. Database lev...