当 LIKE 匹配浮点数时,'\'、'\\'、'\\\' 之间有什么区别?

问题描述

使用原始查询,我尝试匹配下面列中名称为测试表中的“desc”的关键字。

desc 
The progress is above 93% or not
The progress is above 93.1% or not
The progress is above 93.15% or not

使用关键字 '93%' 只匹配 93% 不包括 93.1% 或 93.15% 的完全匹配

SELECT desc FROM test WHERE Replace(desc,' ','') LIKE '%93%%';

我们可以看到所有三个结果。

SELECT desc FROM test WHERE Replace(desc,'') LIKE '%93\%%';

我们可以看到所有三个结果。

SELECT desc FROM test WHERE Replace(desc,'') LIKE '%93\\%%';

这一次,我只能得到 1 个完全匹配的结果。

SELECT desc FROM test WHERE Replace(desc,'') LIKE '%93\\\%%';

这个也和'\\'一样

请解释区别。

解决方法

我唯一的解释是您使用的是古老的 PostgreSQL 版本,或者 standard_conforming_strings 设置为 off

然后您应该会收到警告,除非您已将 escape_string_warning 设置为 off

test=> SET standard_conforming_strings = off;
SET
test=> SELECT 'above 93.15% or not' LIKE '%93%%';
 ?column? 
══════════
 t
(1 row)

test=> SELECT 'above 93.15% or not' LIKE '%93\%%';
WARNING:  nonstandard use of escape in a string literal
LINE 1: SELECT 'above 93.15% or not' LIKE '%93\%%';
                                          ^
HINT:  Use the escape string syntax for escapes,e.g.,E'\r\n'.
 ?column? 
══════════
 t
(1 row)

test=> SELECT 'above 93.15% or not' LIKE '%93\\%%';
WARNING:  nonstandard use of \\ in a string literal
LINE 1: SELECT 'above 93.15% or not' LIKE '%93\\%%';
                                          ^
HINT:  Use the escape string syntax for backslashes,E'\\'.
 ?column? 
══════════
 f
(1 row)

test=> SELECT 'above 93.15% or not' LIKE '%93\\\%%';
WARNING:  nonstandard use of \\ in a string literal
LINE 1: SELECT 'above 93.15% or not' LIKE '%93\\\%%';
                                          ^
HINT:  Use the escape string syntax for backslashes,E'\\'.
 ?column? 
══════════
 f
(1 row)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...