1. 在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的。 这种情况在MySQL的很多版本中都存在。
如果这个问题不解决,那么MySQL将无法实际处理中文。 出现这个问题的原因是因为MySQL在查询字符串时是大小写不敏感的,在编绎MySQL时一般以ISO-8859字符集作为默认的字符集,因此在比较过程中中文编码字符大小写转换造成了这种现象。
2.解决方法:
对于包含中文的字段加上”binary”属性,使之作为二进制比较,例如将”name char(10)”改成”name char(10)binary”。
如果你使用源码编译MySQL,可以编译MySQL时使用 –with–charset=gbk 参数,这样MySQL就会直接支持中文查找和排序了(默认的是latin1)。也可以用 extra-charsets=gb2312,gbk 来加入多个字符集。
3. 如果不想对表结构进行修改或者重新编译MySQL,也可以在查询语句的 order by 部分使用 CONVERT 函数。
比如 select * from mytable order by CONVERT(chineseColumnName USING gbk);
UTF8 默认校对集是 utf8_general_ci , 它不是按照中文来的。你需要强制让MySQL按中文来排序。
例如:
SELECT * FROM employee_in ORDER BY CONVERT(NAME USING gbk)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFUAAAEeCAIAAAB0f84WAAALf0lEQVR4nO2dO27tOBKGuR7DzgTuwZm9ACbEADc0vAZGDAx4AxfojMAFmpEDr8DgSbULZR0o9wQUH8WHjo5nBqOS6ofRPj6WL/yxipSa9bvI/nVusb/OLeY//XM+lfyXr8/zfBA/8RM/8UP+v1/uGLt7uGeMMcbuXv8sV//9cuffYve/Ll+fly/1zIKe1Ot9ujhdmf34Dj+68X9/YuxJXb4+L2+Py4uvz8ubeg8D8fDy+/L1efnz64E9vn99vt6zh5ffl7fHh5fflz+/HpYB+gSv9/exwh/jpp4DQBZVlvif1OXr8/X+8f3r0/Pnl+08BW7hD6G+BM4ef/ru7j9u5A8TYcn2Hv+Xeo4jte+PlfWPMfb4Hle4J3Xxi4JP/vs7xtjz2/Ldh5ffr/eM3f+6vD0yxp7fPv2LoP2OBd3/iJ/4/6L//134Tyv28fHxfUp58IV/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