各位大大,知道mysql里select * from TABLE where column like '%中%',查出来结果不对,怎么解决?
网上有人说把MYSQL里的字符集修改成gb2312,还有种是把查询语句改成select * from TABLE where column binary like '%中%'.

解决方案 »

  1.   

    给你个例子:
    [code]
    /*DDL Information For - test.lkclass*/
    --------------------------------------Table    Create Table                             
    -------  -----------------------------------------
    lkclass  CREATE TABLE `lkclass` (                 
               `id` int(11) NOT NULL,                 
               `classname` varchar(20) DEFAULT NULL,  
               `grade` varchar(20) DEFAULT NULL,      
               `scoreid` int(11) DEFAULT NULL,        
               PRIMARY KEY (`id`)                     
             ) ENGINE=InnoDB DEFAULT CHARSET=gb2312   
    set names gbk;
    select * from lkclass where grade like '%年级%';
    select * from lkclass where grade binary like '%年级%';
    [/code]结果:
    query result(5 records)
    id classname grade scoreid 
    1 一班 三年级 1 
    2 二班 三年级 2 
    3 三班 三年级 3 
    4 四班 三年级 4 
    5 一班 四年级 5 query result(5 records)
    id classname grade scoreid 
    1 一班 三年级 1 
    2 二班 三年级 2 
    3 三班 三年级 3 
    4 四班 三年级 4 
    5 一班 四年级 5 
      

  2.   


    /*DDL Information For - test.lkclass*/
    --------------------------------------Table    Create Table                             
    -------  -----------------------------------------
    lkclass  CREATE TABLE `lkclass` (                 
               `id` int(11) NOT NULL,                 
               `classname` varchar(20) DEFAULT NULL,  
               `grade` varchar(20) DEFAULT NULL,      
               `scoreid` int(11) DEFAULT NULL,        
               PRIMARY KEY (`id`)                     
             ) ENGINE=InnoDB DEFAULT CHARSET=gb2312   
    set names gbk;
    select * from lkclass where grade like '%年级%';
    select * from lkclass where grade binary like '%年级%';
      

  3.   

    加上binary  就可以了。
      

  4.   

    其实是设置表和表内字段的字符类型为GBK就可以了。
      

  5.   

    谢谢各位,我已经解决了,一直没来结贴.呵呵,加上binary就可以了,我在试的时候加错地方了,所以不对.
    select   *   from   TABLE   where   column   binary   like   '%中%'
    这样用就对的,mysql里查一个中文字就会出现这个错误,希望这贴也对新人有用.再次感谢各位!