问题描述
使用原始查询,我尝试匹配下面列中名称为测试表中的“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)