Mysql order by on column with unicode characters

余生长醉 提交于 2019-12-07 12:23:08

问题


I am running a select query on mysql table and trying to order it by the "name" column in the table.
The name column contains both English character names and names with Latin character like â.
I am running into the below problem.
The query I run returns the results ordered in the below manner i.e.
Eg: if Name contains "archer", "aaakash", "â hayden", "bourne", "jason"
The results returned by the query is ordered as below
"aaakash", "archer", "â hayden", "bourne", "jason"

However I want to order it based on unicode code points (like below)
"aaakash", "archer", "bourne", "jason", "â hayden"

(See the difference in the position of â hayden in the orders)
What can I do to order the results based on the character's position in unicode character set?


回答1:


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").



来源:https://stackoverflow.com/questions/18210189/mysql-order-by-on-column-with-unicode-characters

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