【问题标题】:How to change Mysql collation to Turkish?如何将 Mysql 排序规则更改为土耳其语?
【发布时间】:2015-11-10 12:20:21
【问题描述】:

我有一个带有 utf8 字符集和 utf8_unicode_ci 排序规则的 mysql 架构。问题是当我想查询一些土耳其语字符时,Mysql 无法确定该字符是否为土耳其语。

在土耳其语中,“İ”是大写“i”,“I”是大写“ı”。

SELECT name FROM students where name = "testĞÇ"; //returns "testğç" which is true
SELECT name FROM students where name = "testI"; //Mysql thinks "I" character is capital "i" and returns null

我的学生表数据;

testğç
testı

编辑:我的“名称”列的排序规则是 utf8_unicode_ci。

SELECT name FROM students where name = "testI" collate utf8_turkish_ci; //still returns null

编辑 2: 当我将“名称”列的排序规则从 utf8_unicode_ci 更改为 utf8_turkish_ci 时,效果很好。但是这次我需要将所有表格列的排序规则更改为 utf8_turkish_ci,我不知道这会导致任何数据丢失。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    由于 Ğ 和 Ç 正常工作,而 I / İ 不正常,这听起来像是一个 mySQL 错误。

    快速浏览一下 mySQL 论坛,有人推荐使用 Latin5

    http://forums.mysql.com/read.php?147,251229,257016#msg-257016

    【讨论】:

      【解决方案2】:

      使用适当的土耳其语排序规则。

      MariaDB [(none)]> SELECT "testı" = "testI" collate utf8_unicode_ci;
      +--------------------------------------------+
      | "testı" = "testI" collate utf8_unicode_ci  |
      +--------------------------------------------+
      |                                          0 |
      +--------------------------------------------+
      1 row in set (0.00 sec)
      
      MariaDB [(none)]> SELECT "testı" = "testI" collate utf8_turkish_ci;
      +--------------------------------------------+
      | "testı" = "testI" collate utf8_turkish_ci  |
      +--------------------------------------------+
      |                                          1 |
      +--------------------------------------------+
      1 row in set (0.00 sec)
      

      【讨论】:

      • 当我查询您的脚本时,我也得到相同的结果,但问题是我的列的排序规则是 utf8_unicode_ci,当我在脚本末尾添加“collat​​e utf8_turkish_ci”进行查询时,仍然得到空结果。跨度>
      【解决方案3】:

      我知道您没有指定语言,但我认为这可以帮助您。当我遇到和你一样的问题时,我在某个地方找到了一个脚本,并解决了运行此代码的问题:

      <?php 
      $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = '';
      $dbname = 'db_name';
      $collation = "utf8_general_ci";
      $charset = "utf8";
      
      mysql_connect($dbhost,$dbuser,$dbpass);
      mysql_select_db($dbname);
      mysql_query("ALTER DATABASE $dbname COLLATE $collation");
      $result = mysql_query("SHOW TABLES"); 
      while ($row = mysql_fetch_row($result)) { 
          mysql_query("ALTER TABLE $row[0] COLLATE $collation"); 
          $result1 = mysql_query("SHOW COLUMNS FROM $row[0]"); 
          while ($row1 = mysql_fetch_assoc($result1)) { 
              if (preg_match('~char|text|enum|set~', $row1["Type"])) { 
                  mysql_query("ALTER TABLE $row[0] MODIFY $row1[Field] $row1[Type] CHARACTER SET $charset"); 
                  mysql_query("ALTER TABLE $row[0] MODIFY $row1[Field] $row1[Type] COLLATE $collation DEFAULT '$row1[Default]'"); 
                  echo $row[0] . "->" . $row1[Field] . "->" . $row1[Type] . " is now UTF8\n";
              }
          }
      }
      mysql_free_result($result);
      ?>
      

      它只是获取所有表并将其排序规则更改为utf8_general_ci,因为它支持大多数可用的字符。然后您新添加的数据将具有正确的字符集。

      实现这一目的的主要代码是:

      对于数据库:

      ALTER DATABASE [db_name] COLLATE [collation]
      

      对于单个表:

      ALTER TABLE [table_name] MODIFY [column_name] [column_type] CHARACTER SET [charset]
      ALTER TABLE [table_name] MODIFY [column_name] [column_type] COLLATE [collation]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 2014-02-06
        • 2021-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-13
        相关资源
        最近更新 更多