【问题标题】:MySQL wrong ORDER BY for hungarian charactersMySQL 匈牙利字符的 ORDER BY 错误
【发布时间】:2019-08-19 11:20:14
【问题描述】:

我运行以下查询:

SELECT name FROM shops ORDER BY name;

当前结果:

ABC shop
ÁDE shop
ALT shop

预期结果应该是:

ABC shop
ALT shop
ÁDE shop

看起来AÁ 是相等的。 (正确顺序:a

我尝试使用匈牙利排序规则(使用 mysql 8.0.17):

ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_hu_0900_ai_ci;

我发现 mysql 团队已经在 mysql 8.0.1 中修复了这个问题:https://bugs.mysql.com/bug.php?id=12519

SHOW CREATE TABLE 商店;

| shops | CREATE TABLE `shops` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `address_id` bigint(20) unsigned DEFAULT NULL,
  `shop_category_id` bigint(20) unsigned DEFAULT NULL,
  `shop_chain_id` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `partner_id` bigint(20) unsigned DEFAULT NULL,
  `location_id` bigint(20) unsigned DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT '1',
  `user_id` bigint(20) unsigned DEFAULT NULL,
  `deputy_user_id` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `shops_address_id_foreign` (`address_id`),
  KEY `shops_shop_category_id_foreign` (`shop_category_id`),
  KEY `shops_shop_chain_id_foreign` (`shop_chain_id`),
  KEY `shops_partner_id_foreign` (`partner_id`),
  KEY `shops_location_id_foreign` (`location_id`),
  KEY `shops_user_id_foreign` (`user_id`),
  KEY `shops_deputy_user_id_foreign` (`deputy_user_id`),
  CONSTRAINT `shops_address_id_foreign` FOREIGN KEY (`address_id`) REFERENCES `addresses` (`id`) ON DELETE SET NULL,
  CONSTRAINT `shops_deputy_user_id_foreign` FOREIGN KEY (`deputy_user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `shops_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON DELETE SET NULL,
  CONSTRAINT `shops_partner_id_foreign` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE CASCADE,
  CONSTRAINT `shops_shop_category_id_foreign` FOREIGN KEY (`shop_category_id`) REFERENCES `shop_categories` (`id`) ON DELETE SET NULL,
  CONSTRAINT `shops_shop_chain_id_foreign` FOREIGN KEY (`shop_chain_id`) REFERENCES `shop_chains` (`id`) ON DELETE SET NULL,
  CONSTRAINT `shops_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=564 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hu_0900_ai_ci 

【问题讨论】:

  • @Strawberry 问题已更新。
  • @BrettGregson 这个自定义排序看起来有点老套,但我会试试的
  • 为什么是 PHP 标签?您的问题与此无关,除非您想在 PHP 中进行排序。
  • @Peter Kota 不是专家,但它不是 utf8mb4_hu_0900_as_cs 区分重音和大小写的.. 所以它对待

标签: mysql sql character-encoding special-characters collation


【解决方案1】:

没有简单的解决方案:

计划 A:编写自己的排序规则。

B 计划:在 bugs.mysql.com 上写一个错误,抱怨即使在 8.0.17 中,utf8mb4_hu_0900_ai_ci 也是“不正确的”。

(同时,我怀疑 utf8mb4_hungarian_ci 保留了 5.7 排序规则以实现向后兼容性。)

【讨论】:

  • 我看到了您对错误报告的评论;但匈牙利排序规则本身似乎很模棱两可。回顾第一条评论,其中定义了“规则”,它说:“一般来说,在排序或比较时,短版和长版应该被同等对待,除非顺序不能由后面的字符决定元音。”
  • 很难想象这在 mysql 中不起作用。创建自己的排序规则听起来很棒。我会试试。谢谢@RickJames
【解决方案2】:

问题本身是错误的。预期结果是:

ABC shop
ÁDE shop
ALT shop

没错。

https://helyesiras.mta.hu/helyesiras/default/akh12#F2_4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 2011-08-27
    • 2011-11-22
    • 2015-12-28
    • 2011-11-28
    • 1970-01-01
    • 2011-11-27
    • 2017-03-09
    相关资源
    最近更新 更多