Mysql order by on column with unicode characters

谁都会走 提交于 2019-12-05 17:20:27

However I want to order it based on unicode code points (like below)

To sort using unicode code point, you probably need to use utf8_bin collation.

Precisely, the _bin suffix indicate to sort by the binary representation of each character.


To override the default collation while ordering, you will use ORDER BY ... COLLATE:

To paraphrase the documentation:

SELECT k
FROM t1
ORDER BY k COLLATE utf8_bin;

If your text column does not use utf8 encoding, you will have to CONVERT it:

SELECT k
FROM t1
ORDER BY CONVERT(k USING utf8) COLLATE utf8_bin;

Please notice I used utf8 as an example here as this is the most common Unicode encoding. But your MySQL server probably support other Unicode encoding, like ucs2("UTF-16").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!