[英]MySQL: How to perform case-insensitive query
As far as I know LIKE operator should be case insensitive, but if I have the string "ABC Software" and I get the following query: 据我所知,LIKE运算符应该不区分大小写,但是如果我使用字符串“ ABC Software”并且得到以下查询:
SELECT ...
FROM ...
WHERE ... LIKE 'AbC Softwa%'
I get a zero-rows result set (If I upper-case the second letter, the b, I get the right result). 我得到零行结果集(如果我将第二个字母b大写,则得到正确的结果)。 Why?
为什么? Previously I had *utf8_bin* as character encoding, so I switch to *latin_swedish_ci*, supposing that binary matching was the source of all errors, but I get the same problem.
以前我使用* utf8_bin *作为字符编码,所以我切换到* latin_swedish_ci *,并假定二进制匹配是所有错误的根源,但是我遇到了同样的问题。
Try 尝试
SELECT ...
FROM ...
WHERE UPPER(...) LIKE 'ABC SOFTWA%'
MySQL is case-insensitive by default. MySQL默认情况下不区分大小写。
If you want to perform a case- sensitive search use: 如果要执行区分大小写的搜索,请使用:
SELECT ...
FROM ...
WHERE ... LIKE BINARY 'AbC Softwa%'
If your database, or table is configured to be case-sensitive by default, then to perform a case- insensitive search, you need to do something like this: 如果您的数据库或表默认配置为区分大小写,那么要执行不区分大小写的搜索,您需要执行以下操作:
SELECT ...
FROM ...
WHERE ... COLLATE latin1_swedish_ci LIKE 'AbC Softwa%' COLLATE latin1_swedish_ci
If you are using utf8, then try this: 如果您使用的是utf8,请尝试以下操作:
SELECT ...
FROM ...
WHERE ... COLLATE utf8_general_ci LIKE 'AbC Softwa%' COLLATE utf8_general_ci
You might want to investigate why your database is case-sensitive. 您可能想调查为什么数据库区分大小写。
To see how the server is configured, run: 要查看服务器的配置方式,请运行:
SHOW VARIABLES LIKE '%collation%';
To see the collation of your tables, run: 要查看表的排序规则,请运行:
SHOW TABLE STATUS;
And to see the default collation for the database: 并查看数据库的默认排序规则:
SHOW CREATE DATABASE dbname;
where dbname
is the name of the database. 其中
dbname
是数据库的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.