如何使用 LIKE 查询在 PostgreSQL 中找到文字百分号 (%)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33007164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you find a literal percent sign (%) in PostgreSQL using a LIKE query?
提问by magnus
I have character varying
entries in a table where some(not all) values contain percentages, e.g., '12%'
, '97%'
, etc. I want to find all the values that contain percentages. In other words, I want to find all values that end with a percent sign ('%'
).
我有character varying
一个表项,其中一些(不是全部)值包含百分比,如'12%'
,'97%'
等我想找到所有包含百分比值。换句话说,我想找到所有以百分号 ( '%'
)结尾的值。
回答by Rahul Tripathi
You can try like this:
你可以这样试试:
SELECT * FROM my_table WHERE my_column LIKE '%\%%' ESCAPE '\';
Format
格式
<like predicate> ::=
<match value> [ NOT ] LIKE <pattern>
[ ESCAPE <escape character> ]
<match value> ::= <character value expression>
<pattern> ::= <character value expression>
<escape character> ::= <character value expression>
回答by Patrick
You have to escape the literal % sign. By default the escape character is the backslash:
您必须转义文字 % 符号。默认情况下,转义字符是反斜杠:
SELECT * FROM my_table WHERE my_column LIKE '%\%';
In this case the first %
sign matches any starting sequence in my_column
. The remaining \%
are interpreted as a literal % character. The combination is therefore: match anything that ends in a % character.
在这种情况下,第一个%
符号匹配 中的任何起始序列my_column
。其余的\%
被解释为文字 % 字符。因此,组合是:匹配以 % 字符结尾的任何内容。
回答by magnus
I ended up using regular expressions:
我最终使用了正则表达式:
select * from my_table where my_column ~ '%$';
However, I'd still like to know if it's possible using the LIKE
operator/comparison.
但是,我仍然想知道是否可以使用LIKE
运算符/比较。