如何使用 LIKE 通配符在列中搜索(不区分大小写)?

发布于 2024-09-02 11:11:54 字数 216 浏览 2 评论 0原文

我环顾四周,没有找到我想要的东西,所以就这样吧。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

丶情人眼里出诗心の 2024-09-09 11:11:54

我总是使用 lower 来解决这个问题:

SELECT * FROM trees WHERE LOWER( trees.title ) LIKE  '%elm%'

I've always solved this using lower:

SELECT * FROM trees WHERE LOWER( trees.title ) LIKE  '%elm%'
原来分手还会想你 2024-09-09 11:11:54
SELECT  *
FROM    trees
WHERE   trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'

实际上,如果您将COLLATE UTF8_GENERAL_CI 添加到列的定义中,您可以忽略所有这些技巧:它会自动工作。

ALTER TABLE trees 
 MODIFY COLUMN title VARCHAR(…) CHARACTER 
 SET UTF8 COLLATE UTF8_GENERAL_CI. 

这还将重建该列上的所有索引,以便它们可以用于查询而无需前导“%”

SELECT  *
FROM    trees
WHERE   trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'

Actually, if you add COLLATE UTF8_GENERAL_CI to your column's definition, you can just omit all these tricks: it will work automatically.

ALTER TABLE trees 
 MODIFY COLUMN title VARCHAR(…) CHARACTER 
 SET UTF8 COLLATE UTF8_GENERAL_CI. 

This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'

浊酒尽余欢 2024-09-09 11:11:54

这是一个简单的 LIKE 查询的示例:

SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'

现在,使用 LOWER() 函数不区分大小写:

SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')

This is the example of a simple LIKE query:

SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'

Now, case-insensitive using LOWER() func:

SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')
2024-09-09 11:11:54

区分大小写是在列/表/数据库排序规则设置中定义的。您可以通过以下方式在特定排序规则下进行查询:

SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci

例如。

(将 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:

SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci

for instance.

(Replace utf8_general_ci with whatever collation you find useful). The _ci stands for case insensitive.

草莓味的萝莉 2024-09-09 11:11:54

简单地使用:

"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE  '%elm%'";

或使用

"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE  '%elm%'";

两个函数的作用相同

Simply use :

"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE  '%elm%'";

Or Use

"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE  '%elm%'";

Both functions works same

作业与我同在 2024-09-09 11:11:54

我正在做类似的事情。

获取小写值,MySQL 完成剩下的工作

    $string = $_GET['string'];
    mysqli_query($con,"SELECT *
                       FROM table_name
                       WHERE LOWER(column_name)
                       LIKE LOWER('%$string%')");

对于 MySQL PDO 替代方案:

        $string = $_GET['string'];
        $q = "SELECT *
              FROM table_name
              WHERE LOWER(column_name)
              LIKE LOWER(?);";
        $query = $dbConnection->prepare($q);
        $query->bindValue(1, "%$string%", PDO::PARAM_STR);
        $query->execute();

I'm doing something like that.

Getting the values in lowercase and MySQL does the rest

    $string = $_GET['string'];
    mysqli_query($con,"SELECT *
                       FROM table_name
                       WHERE LOWER(column_name)
                       LIKE LOWER('%$string%')");

And For MySQL PDO Alternative:

        $string = $_GET['string'];
        $q = "SELECT *
              FROM table_name
              WHERE LOWER(column_name)
              LIKE LOWER(?);";
        $query = $dbConnection->prepare($q);
        $query->bindValue(1, "%$string%", PDO::PARAM_STR);
        $query->execute();
流年里的时光 2024-09-09 11:11:54

在 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

桃气十足 2024-09-09 11:11:54

使用ILIKE

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';

它对我有用!

use ILIKE

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';

it worked for me !!

半寸时光 2024-09-09 11:11:54

我认为这个查询将进行不区分大小写的搜索:

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';

I think this query will do a case insensitive search:

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
三生殊途 2024-09-09 11:11:54

您不需要ALTER任何表。只需在要使用通配符的实际 SELECT 查询之前使用以下查询:

    set names `utf8`;
    SET COLLATION_CONNECTION=utf8_general_ci;
    SET CHARACTER_SET_CLIENT=utf8;
    SET CHARACTER_SET_RESULTS=utf8;

You don't need to ALTER any table. Just use the following queries, prior to the actual SELECT query that you want to use the wildcard:

    set names `utf8`;
    SET COLLATION_CONNECTION=utf8_general_ci;
    SET CHARACTER_SET_CLIENT=utf8;
    SET CHARACTER_SET_RESULTS=utf8;
夏の忆 2024-09-09 11:11:54

在 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 :)

吾性傲以野 2024-09-09 11:11:54

我一直都是这样解决的:

SELECT * FROM trees WHERE LOWER( trees.title ) LIKE  LOWER('%elm%');

I've always solved like this:

SELECT * FROM trees WHERE LOWER( trees.title ) LIKE  LOWER('%elm%');
泪眸﹌ 2024-09-09 11:11:54
SELECT name 
       FROM gallery 
       WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%' 
       GROUP BY name COLLATE utf8_general_ci LIMIT 5 
SELECT name 
       FROM gallery 
       WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%' 
       GROUP BY name COLLATE utf8_general_ci LIMIT 5 
野味少女 2024-09-09 11:11:54

您必须为表设置正确的编码和排序规则。

表编码必须反映实际的数据编码。你的数据编码是什么?

要查看表编码,您可以运行查询 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

猥︴琐丶欲为 2024-09-09 11:11:54

当我想开发不区分大小写的搜索时,我总是在比较之前将每个字符串转换为小写

When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion

飘过的浮云 2024-09-09 11:11:54

试试这个,它对我有用。

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';

Try this it worked for me.

SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文