Why I need to double-escape (use 4 \\) to find a backslash ( \\ ) in pure SQL?

[亡魂溺海] 提交于 2019-11-29 11:22:28

You escape first for the string syntax, then for LIKE syntax.

In LIKE characters % and _ have special meaning, so if you want to search for literal %, you need to use \%, and if you want to search for literal \% you need to escape the backslash as in \\%.

In string syntax " obviously has special meaning, so if you want to include quote in the string you need to escape it as \", and to include literal \" in the string you have to escape the backslash as in \\".

So in both syntaxes you have to escape \.


If you don't want to use \ to escape the LIKE pattern , you can use ESCAPE keyword. For example:

...  where test LIKE "a\\b%" ESCAPE '|';

This way, you'll need to write |%, |_ or || to escape these special chars.

See String Comparison Functions

The like operator compares against a pattern, which might include % and _. To escape these, you must use \. So backslash is a special character, too.

When you enter the pattern string "a\\\\b" it is interpreted by Mysql and then again by the like operator, wich gives "a\\b" and then "a\b".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!