php 如何在 MySQL 中搜索斜杠 (\)?为什么在 where (=) 不需要转义 (\) 但对于 Like 需要转义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14926386/
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-25 08:13:13  来源:igfitidea点击:

How to search for slash (\) in MySQL? and why escaping (\) not required for where (=) but for Like is required?

phpmysqlsqlescaping

提问by Explosion Pills

Consider this QUERY (DEMO IS HERE)

考虑这个查询(演示在这里

(SELECT * FROM `titles` where title = 'test\')
UNION ALL
(SELECT * FROM `titles` where title LIKE 'test\\')

Output:

输出:

| ID | TITLE |
--------------
|  1 | test\ |
|  1 | test\ |

QUESTION:

题:

Why no extra (\)required for (=)but for (like) additional \\is required? Its clear that MySQL escaped the (test\)with (test\\)then using (test\\\\)is logical for LIKE.

为什么没有多余的(\)的要求(=),但(像)附加\\是必需的?很明显,MySQL用(test\\)转义了(test\),然后使用(test\\\\)对 LIKE 来说是合乎逻辑的。

Table information:

表信息:

CREATE TABLE IF NOT EXISTS `titles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `titles`
--

INSERT INTO `titles` (`id`, `title`) VALUES
(1, 'test\');

回答by Explosion Pills

\functions as an escape character in LIKEby default.

\LIKE默认情况下用作转义字符。

From the manualfor LIKE:

手册LIKE

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 parserand again when the pattern match is made, leaving a single backslash to be matched against.

由于 MySQL 在字符串中使用 C 转义语法(例如,“\n”表示换行符),因此您必须将 LIKE 字符串中使用的任何“\”加倍。例如,要搜索“\n”,请将其指定为“\\n”。要搜索“\”,请将其指定为“\\\\”;这是因为反斜杠被解析器剥离一次,在进行模式匹配时再次剥离,留下一个反斜杠进行匹配。

You can change this by specifying another escape character, as in:

您可以通过指定另一个转义字符来更改此设置,如下所示:

SELECT * FROM `titles` where title LIKE 'test\' ESCAPE '|'

回答by aercolino

Actually, all previous answers have been mangled somewhere. As you can see in the link provided by Karoly Horvath, whose significant bit is reproduced by Explosion Pills, the correct way of searching for 1 backslash (\) is to use 4 backslashes at once (\\\\).

实际上,以前所有的答案都在某处被破坏了。正如您在 Karoly Horvath 提供的链接中所见,其重要位由 Explosion Pills 复制,搜索 1 个反斜杠 (\) 的正确方法是一次使用 4 个反斜杠 (\\\\)

By the way, to show above that single backslash I had to use two at once and to show those four I had to use eight.

顺便说一句,要显示在单个反斜杠上方,我必须一次使用两个,而要显示那四个,我必须使用八个。

回答by Karoly Horvath

LIKEaccepts two wildchar characters, %and _.

LIKE接受两个通配符,%_.

To be able to match these characters, escaping can be used: \%, \_. This also means that if you want to match \, it has to be escaped as well.

为了能够匹配这些字符,可以使用转义:\%, \_. 这也意味着如果你想匹配\,它也必须被转义。

All this is documented in the manual.

所有这些都记录在手册中

回答by Memo

For finding a \within a text field I had to escape the \twice, else the %at the end was found:

为了\在文本字段中找到 a我不得不转义\两次,否则%在最后找到了:

SELECT * FROM `table` where `field` LIKE '%\\%';

回答by Ansar Gondal

If you wanted to remove the (\') from the database column (Test\'s) and replace it with apostrophy(Test's) then you can use the following query.

如果您想从数据库列 (Test\'s) 中删除 (\') 并将其替换为撇号(Test's),那么您可以使用以下查询。

SELECT replace(column_name, "\'", "'") FROM table where column_name  LIKE "%\\%";