Home  >  Article  >  Database  >  How MySQL uses the ESCAPE keyword

How MySQL uses the ESCAPE keyword

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-05-14 09:33:083037browse

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("%", "\%", "%")

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"

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"        ### %_

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete