【问题标题】:Mysql collation issue with information_schema.TABLESInformation_schema.TABLES 的 Mysql 排序规则问题
【发布时间】:2014-04-03 05:47:26
【问题描述】:

我有一个问题:

 select distinct name from 
  (
    select TABLE_NAME as name from information_schema.TABLES 
         where TABLE_SCHEMA ='my_db_name' 
     union 
    select distinct db_table as name from status 
  )t 
  order by name

用mysql(服务器版本:5.1.44)设置

SHOW VARIABLES LIKE 'character_set%';
+--------------------------+------------------------------------------------------+
| Variable_name            | Value                                                |
+--------------------------+------------------------------------------------------+
| character_set_client     | utf8                                                 |
| character_set_connection | utf8                                                 |
| character_set_database   | utf8                                                 |
| character_set_filesystem | binary                                               |
| character_set_results    | utf8                                                 |
| character_set_server     | utf8                                                 |
| character_set_system     | utf8                                                 |
| character_sets_dir       | /Applications/XAMPP/xamppfiles/share/mysql/charsets/ |
+--------------------------+------------------------------------------------------+

SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+

表架构:

CREATE TABLE `status` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `db_table` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `rank` varchar(6) COLLATE utf8_unicode_ci DEFAULT NULL,
  `style_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `db_table` (`db_table`)
) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我进一步更改了 my.cnf

[mysqld]
#
#
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
#

我删除了数据库并重新创建了它,现在我收到错误 Illegal mix of collat​​ions for operation 'UNION' 。请告诉我我做错了什么。我从 php 和 mysql 命令行遇到同样的问题

这行得通:

  select distinct name from 
  (
    select TABLE_NAME collate utf8_unicode_ci as name from information_schema.TABLES 
         where TABLE_SCHEMA ='my_db_name' 
     union 
    select distinct db_table as name from status 
  )t 
  order by name

但我不喜欢这种调整,我的问题是:是否有适当的解决方法。正如您所见,mysql 全局设置已设置为正确的排序规则,但信息架构仍在使用 utf8_general_ci 左右?

【问题讨论】:

  • 这个问题可能是由于你正在做联合系统生成的表(来自于 myisam 的 information_schema)和用户生成的表。

标签: mysql


【解决方案1】:

如果您检查 information_schema 的排序规则,您可能会发现 utf8_general_ci 而不是 utf8_unicode_ci。我不知道为什么。

USE information_schema;
SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+

除了在查询中定义排序规则之外,另一个选项是将您的数据库表更改为使用 utf8_general_ci。 (就个人而言,我更喜欢在查询中指定排序规则)

请记住,每个表上还有一个排序规则设置会覆盖您使用 (SHOW VARIABLES LIKE 'collat​​ion%';) 看到的设置。如果您将所有内容切换到 utf8_general_ci,您可能必须为每个内容运行 ALTER TABLE 语句。如果你有很多表,你也可以做一个mysqldump,find and replace 全部更改,然后重新导入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 2013-01-09
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多