当MySQL执行字符串比较时,它并不区分大小写,但是借助BINARY关键字,MySQL可以执行区分大小写的字符串比较。这是因为BINARY关键字指示MySQL使用其基础ASCII值而不只是字母来比较字符串中的字符。可以使用表'Student_info'中的以下示例的以下示例进行说明-
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 125 | Raman | Shimla | Computers | +------+---------+------------+------------+ 3 rows in set (0.00 sec)
下面的查询将使用BINARY关键字来强制MySQL执行区分大小写的字符串比较。
mysql> select * from student_info WHERE BINARY Name IN('YashPal', 'GAURAV'); +------+---------+----------+---------+ | id | Name | Address | Subject | +------+---------+----------+---------+ | 101 | YashPal | Amritsar | History | +------+---------+----------+---------+ 1 row in set (0.08 sec)
从上面的结果集中可以看出,在使用关键字BINARY之后,MySQL执行区分大小写的字符串比较。