Get the App
SLTechnology News&Howtos  ›  Database  › 

How does MySQL use the ESCAPE keyword

Shulou Source: shulou.com Published: 2022-05-31 22:25:19 10月04日 Update

This article mainly introduces MySQL how to use the ESCAPE keyword, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

MySQL escape

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

In MySQL, escape characters start with "\", and escape characters commonly used in programming are valid in MySQL, so I won't repeat and discuss them here. Here, the function of the ESCAPE keyword is explained mainly through "%" and "_".

%: matches any number of characters.

_: matches a single character.

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

# query users with clear words in their names > SELECT * FROM user WHERE name LIKE CONCAT ("%", "Ming", "%") # query users with% characters in their names > SELECT * FROM user WHERE name LIKE CONCAT ("%", "\%", "%") ESCAPE usage

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

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

It is important to note that all characters referred to by ESCAPE will replace the function of "\" in the query condition.

# suppose there are two users with names of% an 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" #% _ Thank you for reading this article carefully I hope the article "how to use ESCAPE keywords in MySQL" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Character escape name user query key keyword function article sequence beginning semantic valid two value interest at the same time multiple that is common Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Shulou Information Huawei OPPO Reno