mysql-如何使用特殊字符/(正斜杠)和\(反斜杠)处理查询搜索 [英] mysql - How to handle query search with special characters /(forward slash) and \(backslash)

查看:1416
本文介绍了mysql-如何使用特殊字符/(正斜杠)和\(反斜杠)处理查询搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,其中一列允许特殊字符,例如'/'正斜杠和'\'反斜杠.

I am having table where a column allows special characters like '/' forward slash and '\' back slash.

现在,当我尝试从表中搜索此类记录时,我无法获取这些记录.

Now when I am trying to search such records from table, I am unable to get those.

示例::abc \ def或abc/def 我正在生成类似于

example : abc\def or abc/def I am generating search query which is like

select * from table1_1 where column10 like '%abc\def%'

它返回0行,但实际上有1条记录.在这种情况下,如何编写查询,请告诉我.

It is returning 0 rows but actaully there is 1 record existing. how to write a query in this case, let me know.

谢谢.

推荐答案

Barmar部分正确(所以+1)

Barmar is partially correct (so +1),

因此,诀窍是仅对反斜杠进行两次转义,对于字符串转义,仅需进行一次转义.

So the trick is to double escape ONLY the backslash, for string escapes only a single escape is needed.

例如

  • 单引号'仅需转义一次LIKE '%\'%'
  • 但是要查询反斜杠\,您需要加倍转义到LIKE '%\\\\%'
  • 如果要查询反斜杠+单引号\',然后查询LIKE '%\\\\\'%'(带有5个反斜杠)
  • The single quote ' only needs escaping once LIKE '%\'%'
  • But to query backslash \ you need to double escape to LIKE '%\\\\%'
  • If you wanted to query backslash+singlequote \' then LIKE '%\\\\\'%' (with 5 backslashes)

解释源 摘录:

因为MySQL在字符串中使用C转义语法(例如,"\ n" 代表换行符),您必须使用的所有"\" 加倍 在喜欢的字符串中.例如,要搜索"\ n",请将其指定为"\ n". 要搜索"\",请将其指定为"\\";这是因为 反斜杠一次被解析器剥离,当模式被再次剥离 匹配,只剩下一个反斜杠即可匹配.

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-如何使用特殊字符/(正斜杠)和\(反斜杠)处理查询搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆