Home > Database > Mysql Tutorial > How MySQL uses the ESCAPE keyword

How MySQL uses the ESCAPE keyword

醉折花枝作酒筹
Release: 2021-05-14 09:33:08
forward
3137 people have browsed it

This article will introduce to you how MySQL uses the ESCAPE keyword. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How MySQL uses the ESCAPE keyword

MySQL Escape

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

In MySQL, the escape characters start with "\". Common escape characters in programming are all valid in MySQL and will not be described or discussed here. Here, the function of the ESCAPE keyword is mainly explained through "%" and "_".

%: Match any number of characters.

_: Matches a single character.

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

### 查询名字中带明字的用户
> SELECT * FROM user WHERE name LIKE CONCAT("%", "明", "%")

### 查询名字带有%字符的用户
> SELECT * FROM user WHERE name LIKE CONCAT("%", "\%", "%")
Copy after login

Usage of ESCAPE

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

### 查询名字带有“%”字符的用户
> SELECT * FROM user WHERE name LIKE CONCAT("%", "$%", "%") ESCAPE "$"

### 查询名字带有“_”字符的用户
> SELECT * FROM user WHERE name LIKE CONCAT("%", "a_", "%") ESCAPE "a"
Copy after login

It should be noted that all characters referred to by ESCAPE in the query conditions will replace the role of "\".

### 假设存在名字为 %a 和 %_ 两个的用户
> 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"        ### %_
Copy after login

Related recommendations: "mysql tutorial"

The above is the detailed content of How MySQL uses the ESCAPE keyword. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template