如何在 MySQL 中匹配一个字符代替 %?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5247763/
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 to match one character in MySQL in place of %?
提问by Mazhar Ahmed
In the following SQL:
在以下 SQL 中:
SELECT * FROM X WHERE Y LIKE "%s";
the %
will match any characters before an s
. The %
can be x
, xx
, xrf
, etc.
在%
会前一个匹配的任何字符s
。该%
可x
,xx
,xrf
,等。
Is there any symbol that will only match one character, like:
是否有任何符号只能匹配一个字符,例如:
SELECT * FROM X WHERE Y LIKE "?s"
where ?
will match only one character like a
, b
, c
, etc.?
其中,?
仅匹配一个字符一样a
,b
,c
等?
回答by Joe Stefanelli
回答by krtek
_
will match exactly one character :
_
将完全匹配一个字符:
select * from X where Y like "_s"
回答by James
An easy way you can do it if you know the domain of characters to match:
如果您知道要匹配的字符域,则可以采用一种简单的方法:
select * from X where Y like '[a-z]s'
or like this
或者像这样
select * from X where Y like '[a-zA-Z0-9]s'
alternatively you could use the _ (underscore) syntax like follows:
或者,您可以使用 _(下划线)语法,如下所示:
select * from X where Y like '_s'