MySQL 启用 NO_BACKSLASH_ESCAPES 选项时如何转义文字百分号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:47:07  来源:igfitidea点击:

How to escape literal percent sign when NO_BACKSLASH_ESCAPES option is enabled?

mysqlescapingspecial-characters

提问by Kip

My company runs MySQL in NO_BACKSLASH_ESCAPES mode. How can I escape a literal %or _in a LIKE query in this mode? The standard way is \%, but that doesn't work in this mode.

我的公司在 NO_BACKSLASH_ESCAES 模式下运行 MySQL。如何在这种模式下转义文字%_LIKE 查询?标准方法是\%,但在这种模式下不起作用。

Example: a column has the following values: 5% off, 50% off. The following query works in standard mode but not in NO_BACKSLASH_ESCAPES mode:

示例:一列具有以下值:5% off, 50% off. 以下查询适用于标准模式,但不适用于 NO_BACKSLASH_ESCAPES 模式:

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

回答by ajreal

you need escape

你需要逃离

select * from mytable
where mycol like '5\% off' escape '\';

For a version that works regardless of NO_BACKSLASH_ESCAPES mode, you can use a different character, like pipe:

对于无论 NO_BACKSLASH_ESCAES 模式如何都可以工作的版本,您可以使用不同的字符,例如管道:

select * from mytable
where mycol like '5|% off' escape '|';