[英]Like Case Sensitive in MySQL
I have a MySQL query: 我有一个MySQL查询:
SELECT concat_ws(title,description) as concatenated HAVING concatenated LIKE '%SearchTerm%';
And my table is encoded utf8_general_ci with MyISAM. 我的表格用myISAM编码为utf8_general_ci。
Searches seem to be case sensitive. 搜索似乎区分大小写。
I can't figure out how to fix it. 我无法弄清楚如何解决它。 What's going wrong and/or how do I fix it?
出了什么问题和/或我该如何解决?
A much better solution in terms of performance: 在性能方面更好的解决方案:
SELECT .... FROM .... WHERE `concatenated` LIKE BINARY '%SearchTerm%';
String comparision is case-sensitive when any of the operands is a binary string. 当任何操作数是二进制字符串时,字符串比较区分大小写。
Another alternative is to use COLLATE
, 另一种选择是使用
COLLATE
,
SELECT ....
FROM ....
WHERE `concatenated` like '%SearchTerm%' COLLATE utf8_bin;
Try this: 尝试这个:
SELECT LOWER(CONCAT_WS(title,description)) AS concatenated
WHERE concatenated LIKE '%searchterm%'
or (to let you see the difference) 或(让你看到差异)
SELECT LOWER(CONCAT_WS(title,description)) AS concatenated
WHERE concatenated LIKE LOWER('%SearchTerm%')
In this method, you do not have to select the searched field: 在此方法中,您不必选择搜索的字段:
SELECT table.id
FROM table
WHERE LOWER(table.aTextField) LIKE LOWER('%SearchAnything%')
Check CHARSET mentioned in the table schema: 检查表模式中提到的CHARSET:
show create table xyz;
Based on CHARSET, you can try the following. 基于CHARSET,您可以尝试以下方法。
select name from xyz where name like '%Man%' COLLATE latin1_bin;
select name from xyz where name like '%Man%' COLLATE utf8_bin;
Following are the cases which worked for me, CHARSET=latin1, MySQL version = 5.6. 以下是适用于我的案例,CHARSET = latin1,MySQL版本= 5.6。
mysql> select installsrc from appuser where installsrc IS NOT NULL and installsrc like 'Promo%' collate latin1_bin limit 1;
+-----------------------+
| installsrc |
+-----------------------+
| PromoBalance_SMS,null |
+-----------------------+
1 row in set (0.01 sec)
mysql>
mysql> select installsrc from appuser where installsrc IS NOT NULL and installsrc like 'PROMO%' collate latin1_bin limit 1;
+---------------------------+
| installsrc |
+---------------------------+
| PROMO_SMS_MISSEDCALL,null |
+---------------------------+
1 row in set (0.00 sec)
mysql> select installsrc from appuser where installsrc IS NOT NULL and installsrc like 'PROMO%' limit 1;
+-----------------------+
| installsrc |
+-----------------------+
| PromoBalance_SMS,null |
+-----------------------+
1 row in set (0.01 sec)
Just for completion, in case it helps: 只是为了完成,如果有帮助:
As stated on https://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html , for default character sets, nonbinary string comparisons are case insensitive by default. 如https://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html所述 ,对于默认字符集,非二进制字符串比较默认情况下不区分大小写。
Therefore, an easy way to perform case-insensitive comparisons is to cast the field to CHAR, VARCHAR or TEXT type. 因此,执行不区分大小写的比较的简单方法是将字段转换为CHAR,VARCHAR或TEXT类型。
Here is an example with a check against a single field: 以下是针对单个字段进行检查的示例:
SELECT * FROM table1 WHERE CAST(`field1` AS CHAR) LIKE '%needle%';
This is the working code: 这是工作代码:
SELECT title,description
FROM (
SELECT title,description, LOWER(CONCAT_WS(title,description)) AS concatenated
FROM table1
) AS Q
WHERE concatenated LIKE LOWER('%search%')
This works also: 这也有效:
SELECT LOWER(DisplayName) as DN
FROM Bidders
WHERE OrgID=45
HAVING DN like "cbbautos%"
LIMIT 10;
This problem is occurring in this case because of the collation used in the table. 在这种情况下会发生此问题,因为表中使用了排序规则。 You have used
utf8_general_ci
as collation. 您已使用
utf8_general_ci
作为排序规则。 If the collation is changed to utf8_general_ci
then the searches will not be case sensitive. 如果排序规则更改为
utf8_general_ci
则搜索不会区分大小写。 So, one possible solution is to change the collation. 因此,一种可能的解决方案是更改整理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.