如何在MySQL中搜索斜杠(\)?为什么逃避(\)不需要在哪里(=),但对于喜欢是必需的? [英] How to search for slash (\) in MySQL? and why escaping (\) not required for where (=) but for Like is required?

查看:111
本文介绍了如何在MySQL中搜索斜杠(\)?为什么逃避(\)不需要在哪里(=),但对于喜欢是必需的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个QUERY( DEMO在这里

Consider this QUERY (DEMO IS HERE)

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

输出:

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



QUESTION:



(=)需要强度(\),但是(如)需要附加的 \\
它清楚的是,MySQL使用(test\\)转义了(test\),然后使用(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.

表信息:

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\\');


推荐答案

\ LIKE 中作为转义字符,默认情况下。

\ functions as an escape character in LIKE by default.

手册 for LIKE


由于MySQL在字符串中使用C转义语法(例如,\\\
表示换行符),则必须将任何\ 你在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 parser and again when the pattern match is made, leaving a single backslash to be matched against.

您可以通过指定另一个转义字符来更改此值,如:

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

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

这篇关于如何在MySQL中搜索斜杠(\)?为什么逃避(\)不需要在哪里(=),但对于喜欢是必需的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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