使用information_schema.TABLES进行Mysql排序规则问题

时间:2014-04-03 05:47:27

标签: mysql

我有一个问题:

 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
#

我删除了数据库并重新创建了它,现在我收到错误非法混合操作'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左右?

1 个答案:

答案 0 :(得分: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。 (就个人而言,我更喜欢在查询中指定排序规则)

请注意,每个表格上还有一个排序规则设置,它会覆盖您看到的设置(显示变量类型和排序%&#39 ;;)。如果为所有内容切换到utf8_general_ci,则可能必须为每个语句运行ALTER TABLE语句。如果你有很多表,你也可以做一个mysqldump,查找和替换它们全部更改,然后重新导入。