MySQL 查询以选择以特定字符结尾的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13868532/
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
Query to select strings end with certain character
提问by user1810868
I have a column that contains strings as:
我有一个包含字符串的列:
aaa_1
aaa_11
I need to query strings that ends with _1
. I tried the following:
select col from table where col like %_1
;
But the query gives me the strings that ends with _1
and _11
. How can I correct this ?
我需要查询以_1
. 我尝试了以下操作: select col from table where col like %_1
; 但是查询给了我以_1
and结尾的字符串_11
。我该如何纠正?
回答by fthiella
Try this:
尝试这个:
select col from table where col like '%\_1'
character _
is a jolly, like %, but it matches only a single character, so you have to escape it with \
字符_
很有趣,就像 %,但它只匹配一个字符,所以你必须用\
See here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
见这里:http: //dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
回答by Adam Hoffman
You'll want to use something more substring related.
你会想要使用更多与子字符串相关的东西。
Try a where clause like:
尝试一个 where 子句,如:
SELECT col from table WHERE RIGHT(col, 2) = '_1'
回答by Mehdi Karamosly
You should escape %
and _
by adding backslash \
as they are wildcards in mysql:
您应该转义%
并_
添加反斜杠,\
因为它们是 mysql 中的通配符:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
String | Description
\% | Matches one “%” character
\_ | Matches one “_” character
回答by Jonathan
Many of these will pick up things like %_12
or %_111121
, etc. Make sure you test out various cases to be sure these SELECT
statements are giving you the proper subset.
其中许多会选择诸如%_12
或 之类的东西%_111121
。确保您测试了各种情况,以确保这些SELECT
语句为您提供了正确的子集。
回答by Elaine
SELECT col FROM table WHERE col REGEXP '_1[[:>:]]'