经过测试,在Oracle中不能再使用单引号“’”来转义“%”,“_”了,而是用反斜杠“\”,不仅如此,还要声明反斜杠“\”是转义字符。
假设表为tb,字段为col,要在col中查询包含“%”或“_”字符的字段,sql如下:
-- Oracle
select * from tb where col like '%\%%' or col like '%\_%' escape '\'
sql中“escape”表明声明反斜杠“\”为转义字符,紧接在“\”后的“%”就表示“%”符号,而左右两边的“%”表示模糊查询占位符。
注意:“%”,“_”作为占位符只会出现在where之后,如果是select ‘%’ from dual就不用转义了。
而MySQL中也是大同小异,不过MySQL不用写“escape”了,直接使用反斜杠“\”就可以了。
-- MySQL
select * from tb where col like '%\%%' or col like '%\_%'
最后再来测试下单引号“’”,测试发现Oracle中依然只能使用单引号“’”作为单引号“’”的转义字符,而MySQL中可以使用单引号“’”和反斜杠“\”作为单引号“’”的转义字符。
sql如下:
-- Oracle select '''' from dual; select * from tb where col like '%''%' -- MySQL select '''' from dual; select * from tb where col like '%''%' -- or select '\'' from dual; select * from tb where col like '%\'%' |