mysql默认情况下查询语句的where条件是不区分大小写的。 建表语句CREATETABLEtestusers(idbigint(20)unsignedNOTNULLAUTOINCREMENT,namevarchar(30)NOTNULLDEFAULTCOMMENT姓名,PRIMARYKEY(id),KEYidxname(name))ENGINEInnoDBDEFAULTCHARSETutf8mb4COMMENT用户表; 初始化语句INSERTINTOtestusers(id,name)VALUES(1,Abc),(2,abc),(3,sss); 默认不区分大小写selectfromtestuserswherenameabc; 输出结果 id name 1hrAbc 2hrabc索引情况explainselectfromtestuserswherenameabc; 索引结果: id selecttype table partitions type possiblekeys key keylen ref rows filtered Extra 1hrSIMPLE testusers NULL ref idxname idxname 122hrconst 2hr100hrUsingindex 命中了索引indexname,并且typeconst区分大小写的方法 区分大小写的方法有3种:字段前添加关键字:binary建表时字段加入关键字:binary修改字段字符集:方案一:查询条件字段前添加关键字:binary 查询语句selectfromtestuserswherebinarynameabc; 输出结果 id name 2hrabc查询语句索引情况explainselectfromtestuserswherebinarynameabc; 输出结果: id selecttype table partitions type possiblekeys key keylen ref rows filtered Extra 1hrSIMPLE testusers NULL index NULL idxname 122hrNULL 3hr100hrUsingwhere;Usingindex 可以看出来命中了索引idxname,并且typeindex方案二:建表时字段加入关键字:binaryCREATETABLEtestusers(idbigint(20)unsignedNOTNULLAUTOINCREMENT,namevarchar(30)binaryNOTNULLDEFAULTCOMMENT姓名,PRIMARYKEY(id),KEYidxname(name))ENGINEInnoDBDEFAULTCHARSETutf8mb4COMMENT用户表; 查询语句selectfromtestuserswherenameabc; 输出结果 id name 2hrabc查询语句索引情况explainselectfromtestuserswherenameabc; 输出结果: id selecttype table partitions type possiblekeys key keylen ref rows filtered Extra 1hrSIMPLE testusers NULL ref idxname idxname 122hrconst 1hr100hrUsingindex 可以看出命中了idxname索引,并且typeref方案三:修改字段的字符序(collation) 字符集(characterset)和字符序(collation)是两个东西,一个字符集对应1个或者多个字符序。 可以通过SHOWCOLLATION查看数据库支持的字符序,通过SHOWCHARACTERSET字符集SHOWCOLLATIONwherecollationlikeutf8mb4命令 Collation Charset Id Default Compiled Sortlen utf8mb4generalci utf8mb4 45hrYes Yes 1hrutf8mb4bin utf8mb4 46hrYes 1hrutf8mb4unicodeci utf8mb4 224hrYes 8hrutf8mb4icelandicci utf8mb4 225hrYes 8hrutf8mb4latvianci utf8mb4 226hrYes 8hrutf8mb4romanianci utf8mb4 227hrYes 8hrutf8mb4slovenianci utf8mb4 228hrYes 8hrutf8mb4polishci utf8mb4 229hrYes 8hrutf8mb4estonianci utf8mb4 230hrYes 8hrutf8mb4spanishci utf8mb4 231hrYes 8hrutf8mb4swedishci utf8mb4 232hrYes 8hrutf8mb4turkishci utf8mb4 233hrYes 8hrutf8mb4czechci utf8mb4 234hrYes 8hrutf8mb4danishci utf8mb4 235hrYes 8hrutf8mb4lithuanianci utf8mb4 236hrYes 8hrutf8mb4slovakci utf8mb4 237hrYes 8hrutf8mb4spanish2ci utf8mb4 238hrYes 8hrutf8mb4romanci utf8mb4 239hrYes 8hrutf8mb4persianci utf8mb4 240hrYes 8hrutf8mb4esperantoci utf8mb4 241hrYes 8hrutf8mb4hungarianci utf8mb4 242hrYes 8hrutf8mb4sinhalaci utf8mb4 243hrYes 8hrutf8mb4german2ci utf8mb4 244hrYes 8hrutf8mb4croatianci utf8mb4 245hrYes 8hrutf8mb4unicode520ci utf8mb4 246hrYes 8hrutf8mb4vietnameseci utf8mb4 247hrYes 8hr常用的字符序:utf8generalciutf8mb4generalci:不区分大小写utf8binutf8mb4bin:区分大小写 建表语句:CREATETABLEtestusers3(idbigint(20)unsignedNOTNULLAUTOINCREMENT,namevarchar(30)CHARACTERSETutf8mb4COLLATEutf8mb4binNOTNULLDEFAULTCOMMENT姓名,PRIMARYKEY(id),KEYidxname(name))ENGINEInnoDBDEFAULTCHARSETutf8mb4COMMENT用户表; 查询语句:selectfromtestuserswherenameabc; 输出结果 id name 2hrabc查询语句索引情况explainselectfromtestuserswherenameabc; 输出结果: id selecttype table partitions type possiblekeys key keylen ref rows filtered Extra 1hrSIMPLE testusers NULL index NULL idxname 122hrNULL 3hr100hrUsingwhere;Usingindex 命中了索引idxname,并且typeindex