教您如何将不同的MySQL字符集转化成统一的字符集

数据库 MySQL
不同的MySQL字符集如何才能转化成统一的字符集呢?这是很值得我们考虑的问题,下文就教您一个方法,可以轻松解决这个问题。

下面为您介绍的MySQL字符集处理方法是将不同的MySQL字符集,转化成统一的字符集。 该方法供您参考,希望对您学习MySQL字符集方面能有所启迪。

  1. After   an   upgrade   to   MySQL   4.1,   the   statement   fails:        
  2.  
  3. mysql>   SELECT   SUBSTRING_INDEX(USER(),'@',1);      
  4.  
  5. ERROR   1267   (HY000):   Illegal   mix   of   collations      
  6.  
  7. (utf8_general_ci,IMPLICIT)   and   (latin1_swedish_ci,COERCIBLE)      
  8.  
  9. for   operation   'substr_index'      
  10.  
  11. The   reason   this   occurs   is   that   usernames   are   stored   using   UTF8   (see   section   11.6   UTF8   for   Metadata).   As   a   result,   the   USER()   function   and   the   literal   string   '@'   have   different   character   sets   (and   thus   different   collations):        
  12.  
  13. mysql>   SELECT   COLLATION(USER()),   COLLATION('@');      
  14.  
  15. +-------------------+-------------------+      
  16.  
  17. |   COLLATION(USER())   |   COLLATION('@')         |      
  18.  
  19. +-------------------+-------------------+      
  20.  
  21. |   utf8_general_ci       |   latin1_swedish_ci   |      
  22.  
  23. +-------------------+-------------------+      
  24.  
  25. One   way   to   deal   with   this   is   to   tell   MySQL to   interpret   the   literal   string   as   utf8:        
  26.  
  27. mysql>   SELECT   SUBSTRING_INDEX(USER(),_utf8'@',1);      
  28.  
  29. +------------------------------------+      
  30.  
  31. |   SUBSTRING_INDEX(USER(),_utf8'@',1)   |      
  32.  
  33. +------------------------------------+      
  34.  
  35. |   root                                                               |      
  36.  
  37. +------------------------------------+      
  38.  
  39. Another   way   is   to   change   the   connection   character   set   and   collation   to   utf8.   You   can   do   that   with   SET   NAMES   'utf8'   or   by   setting   the   character_set_connection   and   collation_connection   system   variables   directly.        
  40.  

表的编码转换可以用:(MySQL   Version   >   4.12)   

  1. ALTER   TABLE   tbl_name   CONVERT   TO   CHARACTER   SET   charset_name;      
  2.  

之前的版本可以用:   

  1. ALTER   TABLE   tbl_name   CHARACTER   SET   charset_name;   
  2.  

 

 

 

【编辑推荐】

深入研究MySQL删除多表数据

多个MySQL表结果集组合

MySQL分表处理的实现方法

MySQL授权表使用示例

MySQL内存表的弊端

责任编辑:段燃 来源: 互联网
相关推荐

2010-06-02 17:59:19

MySQL字符集

2010-10-08 09:51:52

Mysql设置字符

2010-11-01 15:44:45

DB2字符集

2010-04-30 10:16:22

Oracle字符集

2010-06-07 16:09:58

MySQL字符集

2020-12-16 06:34:16

MySQL字符集服务器

2021-03-23 18:21:30

MySQL数据库字符集

2011-04-11 10:59:33

Oracle字符集

2010-06-13 16:17:26

MySQL改变字符集

2009-02-24 10:51:30

2011-05-16 13:15:55

MySQL存储字符集

2011-04-11 11:05:12

Oracle字符集

2010-10-09 10:57:22

MySQL默认字符集

2010-10-09 10:49:07

MySQL默认字符集

2010-10-14 11:52:38

MySQL字符集

2010-05-17 10:01:09

MySql字符集

2010-10-09 11:12:32

mysql默认字符集

2010-10-09 11:31:25

MySQL字符集

2009-11-20 16:52:35

Oracle字符集

2009-07-01 00:23:40

MySQL字符集乱码
点赞
收藏

51CTO技术栈公众号