我有一个函数,返回5个混合大小写字符。如果我对这个字符串进行查询,不管大小写,它都会返回值。

如何使MySQL字符串查询区分大小写?


当前回答

克雷格·怀特(Craig White)发布的答案有很大的性能损失

SELECT *  FROM `table` WHERE BINARY `column` = 'value'

因为它不使用索引。所以,要么你需要像这里提到的那样改变表的排序规则https://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html。

OR

最简单的解决方法,你应该使用二进制值。

SELECT *  FROM `table` WHERE `column` = BINARY 'value'

E.g.

mysql> EXPLAIN SELECT * FROM temp1 WHERE BINARY col1 = "ABC" AND col2 = "DEF" ;
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | temp1  | ALL  | NULL          | NULL | NULL    | NULL | 190543 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+

VS

mysql> EXPLAIN SELECT * FROM temp1 WHERE col1 = BINARY "ABC" AND col2 = "DEF" ;
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                              |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
|  1 | SIMPLE      | temp1 | range | col1_2e9e898e | col1_2e9e898e | 93      | NULL |    2 | Using index condition; Using where |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
enter code here

set中的1行(0.00秒)

其他回答

不使用=运算符,您可能希望使用LIKE或LIKE二进制

// this returns 1 (true)
select 'A' like 'a'

// this returns 0 (false)
select 'A' like binary 'a'


select * from user where username like binary 'a'

在它的条件下,它会取a和不取a

Mysql默认情况下不区分大小写,请尝试将语言排序规则更改为latin1_general_cs

太好了!

我和你们分享一个比较密码的函数的代码:

SET pSignal =
(SELECT DECODE(r.usignal,'YOURSTRINGKEY') FROM rsw_uds r WHERE r.uname =
in_usdname AND r.uvige = 1);

SET pSuccess =(SELECT in_usdsignal LIKE BINARY pSignal);

IF pSuccess = 1 THEN
      /*Your code if match*/
ELSE
      /*Your code if don't match*/

END IF;

克雷格·怀特(Craig White)发布的答案有很大的性能损失

SELECT *  FROM `table` WHERE BINARY `column` = 'value'

因为它不使用索引。所以,要么你需要像这里提到的那样改变表的排序规则https://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html。

OR

最简单的解决方法,你应该使用二进制值。

SELECT *  FROM `table` WHERE `column` = BINARY 'value'

E.g.

mysql> EXPLAIN SELECT * FROM temp1 WHERE BINARY col1 = "ABC" AND col2 = "DEF" ;
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | temp1  | ALL  | NULL          | NULL | NULL    | NULL | 190543 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+

VS

mysql> EXPLAIN SELECT * FROM temp1 WHERE col1 = BINARY "ABC" AND col2 = "DEF" ;
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                              |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
|  1 | SIMPLE      | temp1 | range | col1_2e9e898e | col1_2e9e898e | 93      | NULL |    2 | Using index condition; Using where |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
enter code here

set中的1行(0.00秒)

下面是MySQL版本等于或高于5.5的情况。

添加到/etc/mysql/my.cnf

  [mysqld]
  ...
  character-set-server=utf8
  collation-server=utf8_bin
  ...

我尝试的所有其他排序似乎都不区分大小写,只有“utf8_bin”有效。

在这之后不要忘记重新启动mysql:

   sudo service mysql restart

根据http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html,还有一个“latin1_bin”。

“utf8_general_cs”不被mysql启动接受。(我把“_cs”读成“区分大小写”- ??)