如何在MySQL中搜索斜杠(\)?以及为什么转义 (\) 对于 where (=) 不是必需的,但对于 Like 是必需的?
考虑此查询(演示在这里)
(SELECT * FROM `titles` where title = 'test\\')
UNION ALL
(SELECT * FROM `titles` where title LIKE 'test\\\\')
输出:
| ID | TITLE |
--------------
| 1 | test\ |
| 1 | test\ |
问题:
为什么 (=) 不需要额外的 (\),而对于 (like) 需要额外的 \\?很明显,MySQL用(test\\)转义了(test\\),然后使用(test\\\\)对于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\\');