MySQL查询时区分大小写(转)

说明:在MySQL查询时要区分大小写会涉及到两个概念character set和collation,这两个概念在表设计时或者在查询时都可以指定的,详细参考:http://www.cnblogs.com/EasonJim/p/8128196.html

MySQL查询默认是不区分大小写的 如:

select * from some_table where str=‘abc';
select * from some_table where str='ABC';

得到的结果是一样的,如果我们需要进行区分的话可以按照如下方法来做: 

第一种方法:

要让MySQL查询区分大小写,可以:

select * from some_table where binary str='abc'
select * from some_table where binary str='ABC'

第二方法:

在建表时时候加以标识

create table some_table(
   str char(20) binary
)

原理:

对于char、varchar和text类型,binary属性可以为列分配该列字符集的 校对规则。binary属性是指定列字符集的二元校对规则的简写。排序和比较基于数值字符值。因此区分了大小写。

参考:

http://www.jb51.net/article/70884.htm(以上内容转自此篇文章)

http://blog.csdn.net/wangcunhuazi/article/details/46366657

https://www.cnblogs.com/trying/p/3669101.html

原文地址:https://www.cnblogs.com/EasonJim/p/8289461.html