查询以选择以某些字符结尾的字符串
问题描述:
我有一列包含以下字符串:
I have a column that contains strings as:
aaa_1
aaa_11
我需要查询以_1
结尾的字符串.我尝试了以下方法:
从表中选择col,其中col如%_1
;
但是查询给了我以_1
和_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 ?
答
尝试一下:
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 \
请参见此处: http://dev .mysql.com/doc/refman/5.0/zh-CN/string-comparison-functions.html#operator_like