关于数据库搜索,带有反斜杠的数据like%%搜索不出?

例:数据库里有一条数据,id为1,name为D\F
运行sql语句:select * from table where name like '%D\\F%'
为什么得不到结果??

SQL Server数据库,在like语句中,默认没有转义字符的,故你这里的like语句中的字符串只要一个反斜杠就够了:

select * from table where name like '%D\F%'

而如果要使用转义字符,则用escape来指定,例如:

select * from table where name like '%D\\F%' escape '\'

追问

MySQL数据库呢?

追答

换了电脑,没能在mysql测试。你试试这样:

select * from table where name like '%D\\\\F%'

即用4个反斜杠代表一个反斜杠字符。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-10
这样改
select * from table where name like '%D\F%'
试一下追问

不行额。

select * from table where name = 'D\F' 不行
select * from table where name = 'D\\F' 行
select * from table where name like '%D\F%' 不行
select * from table where name like '%D\\F%' 不行