MySQL 从搜索问题开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9605999/
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
MySQL starts with searching issue
提问by Jamie Hartnoll
I'm having an issue using the %
wildcard with a MySQL query.
我在%
MySQL 查询中使用通配符时遇到问题。
http://www.w3schools.com/sql/sql_like.asp
http://www.w3schools.com/sql/sql_like.asp
Having read that article, I am using %
and not getting quite what I was expecting.
读过那篇文章后,我正在使用%
并且没有得到我所期望的。
I have a series of values, such as
我有一系列的价值观,比如
1_1
1_2
2_1
2_2... etc
including
包含
11_1
11_2
Now, in some cases I want to return specifically those whose value = 11_2
, or 1_2
etc. This works fine
现在,在某些情况下,我想特别返回那些值= 11_2
,或1_2
等这工作正常
WHERE fieldName = '11_2'
etc... as expected
等等......正如预期的那样
However, in some cases I want to find all items which start with a 1
or all items which start with 11
但是,在某些情况下,我想查找以 a 开头的1
所有项目或所有以11
From the w3Schools link, I was expecting
从 w3Schools 链接,我期待
WHERE fieldName LIKE '1_%'
To find anything that begins with 1_
specifically, therefore, in my example, returning:
要找到任何以1_
特定开头的内容,因此,在我的示例中,返回:
1_1
1_2
BUT, it also returns
但是,它也返回
11_1
11_2
Why is this? And how can I over come it?
为什么是这样?我怎么能克服呢?
回答by markblandford
Underscore is a wildcard for a single character. You will need to change your SQL to something like:
下划线是单个字符的通配符。您需要将 SQL 更改为以下内容:
WHERE fieldName LIKE '1%'
Or you can escape the underscore
或者你可以转义下划线
WHERE fieldName LIKE '1\_%'