简体   繁体   中英

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

I have a field title of type json that contains translations for different locales. 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 і, а, б, я . For other text fields (not json) ordering works as expected а, б, і, я

Additional info

Mysql version: 5.7.25

database collation: 'utf8mb4_unicode_ci'

It turns out that the collation of database and the collation of json operators are different:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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