mysql unicode排序语句,Mysql按带有unicode字符的列排序

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?

解决方案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").

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值