简体   繁体   English

MySQL的。 json文件错误的顺序包含utf-8字符串

[英]MySQL. Wrong order by json fied containing utf-8 string

I have a field title of type json that contains translations for different locales. 我有一个json类型的字段title ,其中包含不同语言环境的翻译。 It looks like 看起来像

{'en'=>'Title', 'uk'=>'Заголовок'}

I'm trying to order records by a translation 我正在尝试通过翻译订购记录

select id, slug, title->>'$.uk' as locale_title from blog_posts
order by locale_title

It works for en locale with Latin symbols but for uk (Ukrainian) locale with Cyrillic symbols I get the wrong order like і, а, б, я . 它适用于en区域与拉丁符号,但对于uk西里尔字母符号(乌克兰)语言环境,我得到了错误的顺序一样і, а, б, я For other text fields (not json) ordering works as expected а, б, і, я 对于其他文本字段(不是json),排序按预期进行а, б, і, я

Additional info 附加信息

Mysql version: 5.7.25 MySQL版本:5.7.25

database collation: 'utf8mb4_unicode_ci' 数据库整理:'utf8mb4_unicode_ci'

It turns out that the collation of database and the collation of json operators are different: 事实证明,数据库的排序规则和json运算符的排序规则是不同的:

collation(title->>'$.uk') //utf8mb4_bin
collation(other_field) //utf8mb4_unicode_ci

To fix my problem I should set utf8mb4_unicode_ci collation for json value explicitly: 为了解决我的问题,我应该为json值显式设置utf8mb4_unicode_ci排序规则:

select id, slug, title->>'$.uk' as locale_title from blog_posts
order by locale_title collate utf8mb4_unicode_ci

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM