MySQL中文排序錯誤的解決方案

來源:互聯網
上載者:User

MySQL中文排序錯誤的解決方案
/*

方法一

直接將欄位的字元集改成gbk,然後直接使用order by 排序即可。

mysql教程 5.x支援某列單獨定義字元集

方法二

對於包含中文的欄位加上"binary"屬性,使之作為二進位比較,例如將"name char(10)"改成"name char(10)binary"。
如果你使用源碼教程編譯MySQL,可以編譯MySQL時使用 --with--charset=gbk 參數,這樣MySQL就會直接支援中文尋找和排序了(預設的是latin1)。也可以用 extra-charsets=gb2312,gbk 來加入多個字元集

下面來看個執行個體

1)test1:
CREATE TABLE `test1` (                                 
          `id` int(4) unsigned NOT NULL auto_increment,       
          `name` varchar(20) character set utf8 default NULL,
          PRIMARY KEY (`id`)                                 
        ) ;

insert into `test1` (`id`, `name`) values('1','鄧www.111cn.net');
insert into `test1` (`id`, `name`) values('2','李');
insert into `test1` (`id`, `name`) values('3','江');
insert into `test1` (`id`, `name`) values('4','胡');

select name from test1 order by name;

得出的結果是不正確的,現在我們用

test1表中的資料沒有按照中文拼音排序,而使用了gbk字元集的test2查詢結果令人很滿意。

select * from core_vender_info order by convert(vender_abbrev USING gbk) COLLATE gbk_chinese_ci

查詢的時候,通過convert函數,把查詢出來的資料使用的字元集gb2312編碼就可以了

*/
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.