如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:02:32  来源:igfitidea点击:

How to match one character in MySQL in place of %?

mysql

提问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。该%xxxxrf,等。

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.?

其中,?仅匹配一个字符一样abc等?

回答by Joe Stefanelli

You want to use an underscore (_) character. See the documentation here.

您想使用下划线 ( _) 字符。请参阅此处的文档。

回答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'