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
How to escape literal percent sign when NO_BACKSLASH_ESCAPES option is enabled?
提问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 '|';