我有一个表,其中一列允许使用特殊字符,例如“/”(正斜杠)和“”(反斜杠)。
现在,当我尝试从表中搜索此类记录时,我无法获得这些记录。
例如:abc\def 或 abc/def
我正在生成如下搜索查询:
select * from table1_1 where column10 like '%abc\def%'
它返回 0 行,但实际上存在 1 条记录应返回。在这种情况下,我该如何编写查询?
最佳答案
诀窍是只对反斜杠进行双重转义;对于字符串转义,只需要一次转义。
例如
- 单引号
'
只需要转义一次LIKE '%\'%'
- 但是要查询反斜杠
\
你需要双重转义到LIKE '%\\\\%'
- 如果你想查询反斜杠+单引号
\'
那么LIKE '%\\\\\'%'
(有 5 个反斜杠)
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\n”. To search for “\”, specify it as “\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
关于mysql - 如何处理带有特殊字符/(正斜杠)和\(反斜杠)的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16490325/