如何使用 LIKE 通配符在列中搜索(不区分大小写)?
我环顾四周,没有找到我想要的东西,所以就这样吧。
SELECT * FROM trees WHERE trees.`title` LIKE '%elm%'
这工作正常,但如果树被命名为 Elm 或 ELM 等,则不行...
如何使 SQL 对此通配符搜索不区分大小写?
我正在使用 MySQL 5 和 Apache。
I looked around some and didn't find what I was after so here goes.
SELECT * FROM trees WHERE trees.`title` LIKE '%elm%'
This works fine, but not if the tree is named Elm or ELM etc...
How do I make SQL case insensitive for this wild-card search?
I'm using MySQL 5 and Apache.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我总是使用 lower 来解决这个问题:
I've always solved this using lower:
实际上,如果您将
COLLATE UTF8_GENERAL_CI
添加到列的定义中,您可以忽略所有这些技巧:它会自动工作。这还将重建该列上的所有索引,以便它们可以用于查询而无需前导“%”
Actually, if you add
COLLATE UTF8_GENERAL_CI
to your column's definition, you can just omit all these tricks: it will work automatically.This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'
这是一个简单的 LIKE 查询的示例:
现在,使用 LOWER() 函数不区分大小写:
This is the example of a simple LIKE query:
Now, case-insensitive using LOWER() func:
区分大小写是在列/表/数据库排序规则设置中定义的。您可以通过以下方式在特定排序规则下进行查询:
例如。
(将
utf8_general_ci
替换为您认为有用的任何排序规则)。_ci
代表不区分大小写。The case sensitivity is defined in the columns / tables / database collation settings. You can do the query under a specific collation in the following way:
for instance.
(Replace
utf8_general_ci
with whatever collation you find useful). The_ci
stands for case insensitive.简单地使用:
或使用
两个函数的作用相同
Simply use :
Or Use
Both functions works same
我正在做类似的事情。
获取小写值,MySQL 完成剩下的工作
对于 MySQL PDO 替代方案:
I'm doing something like that.
Getting the values in lowercase and MySQL does the rest
And For MySQL PDO Alternative:
在 MySql 中,非二进制字符串比较(包括
LIKE
)默认不区分大小写:https://dev.mysql.com/doc/refman/en/case -sensitivity.html
Non-binary string comparisons (including
LIKE
) are case insensitive by default in MySql:https://dev.mysql.com/doc/refman/en/case-sensitivity.html
使用
ILIKE
它对我有用!
use
ILIKE
it worked for me !!
我认为这个查询将进行不区分大小写的搜索:
I think this query will do a case insensitive search:
您不需要
ALTER
任何表。只需在要使用通配符的实际SELECT
查询之前使用以下查询:You don't need to
ALTER
any table. Just use the following queries, prior to the actualSELECT
query that you want to use the wildcard:在 mysql 5.5 中, like 运算符不敏感...所以如果您的值是 elm 或 ELM 或 Elm 或 eLM 或任何其他,并且您使用 like '%elm%' ,它将列出所有匹配的值。
我不能说早期版本的mysql。
如果你进入 Oracle ,就像区分大小写一样,所以如果你输入像 '%elm%' 这样的内容,它只会为此而忽略大写。
奇怪,但事实就是这样:)
well in mysql 5.5 , like operator is insensitive...so if your vale is elm or ELM or Elm or eLM or any other , and you use like '%elm%' , it will list all the matching values.
I cant say about earlier versions of mysql.
If you go in Oracle , like work as case-sensitive , so if you type like '%elm%' , it will go only for this and ignore uppercases..
Strange , but this is how it is :)
我一直都是这样解决的:
I've always solved like this:
您必须为表设置正确的编码和排序规则。
表编码必须反映实际的数据编码。你的数据编码是什么?
要查看表编码,您可以运行查询
SHOW CREATE TABLE tablename
You must set up proper encoding and collation for your tables.
Table encoding must reflect the actual data encoding. What is your data encoding?
To see table encoding, you can run a query
SHOW CREATE TABLE tablename
当我想开发不区分大小写的搜索时,我总是在比较之前将每个字符串转换为小写
When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion
试试这个,它对我有用。
Try this it worked for me.