Home > Database > Mysql Tutorial > How to Escape Percent and Underscore Characters in MySQL\'s NO_BACKSLASH_ESCAPES Mode?

How to Escape Percent and Underscore Characters in MySQL\'s NO_BACKSLASH_ESCAPES Mode?

Mary-Kate Olsen
Release: 2024-12-01 00:08:11
Original
773 people have browsed it

How to Escape Percent and Underscore Characters in MySQL's NO_BACKSLASH_ESCAPES Mode?

Escaping Literal Percent and Underscore Characters in MySQL with NO_BACKSLASH_ESCAPES Mode

When MySQL is running in NO_BACKSLASH_ESCAPES mode, the standard method of escaping literal percent (%) and underscore (_) characters using the backslash () character is not applicable. This article provides alternative ways to escape these characters specifically in this mode.

Issue

Consider the following example: A column in a MySQL table contains values such as "5% off" and "50% off." The following LIKE query, which uses the standard backslash escape character, will not work in NO_BACKSLASH_ESCAPES mode:

SELECT * FROM mytable
WHERE mycol LIKE '5\% off'
Copy after login

Solution

In NO_BACKSLASH_ESCAPES mode, you can escape percent and underscore characters using a different escape sequence. One option is to use the backslash character preceded by the escape keyword. For example:

SELECT * FROM mytable
WHERE mycol LIKE '5\% off' ESCAPE '\'
Copy after login

Alternative Solution

Alternatively, you can use a different character for escaping, such as the pipe (|) character. The query below will work regardless of the NO_BACKSLASH_ESCAPES mode setting:

SELECT * FROM mytable
WHERE mycol LIKE '5|% off' ESCAPE '|'
Copy after login

By using these techniques, you can effectively escape literal percent and underscore characters in MySQL queries even when in NO_BACKSLASH_ESCAPES mode.

The above is the detailed content of How to Escape Percent and Underscore Characters in MySQL\'s NO_BACKSLASH_ESCAPES Mode?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template