mysql的正确排序规则_MySQL排序顺序-排序规则?

bd96500e110b49cbb3cd949968f18be7.png

I'm having a difficult time sorting a char field in MySQL. The problem is that accented characters get mixed up with un-accented characters. For example:

Abc

Ábd

Acc

I thought it may have something to do with collation. So I changed the collation of my table to utf8-ut8_bin, after reading this post. Actually, I altered the table several times to various collations. No cigar.

I should also add that, I don't mind the order of the sort as long as the sort doesn't result in a mixed list. In other words, this is fine:

Ábd

Abc

Acc

and so is this:

Abc

Acc

Ábd

Looking forward to your response.

解决方案

You just need to use a case-sensitive collation, for example: utf8_general_cs.

UPD

I am sorry, it seems there is no utf8_general_cs, utf8_bin should work though.

And you should change the collation of the specific field instead of that of the table (or be sure that the field does use the table defaults).

mysql> SELECT * FROM (

-> SELECT 'A' as l

-> UNION ALL

-> SELECT 'á' as l

-> UNION ALL

-> SELECT 'A' as l) ls

-> ORDER BY l;

+----+

| l |

+----+

| A |

| á |

| A |

+----+

3 rows in set (0.00 sec)

mysql> SELECT * FROM (

-> SELECT 'A' as l

-> UNION ALL

-> SELECT 'á' as l

-> UNION ALL

-> SELECT 'A' as l) ls

-> ORDER BY l COLLATE utf8_bin;

+----+

| l |

+----+

| A |

| A |

| á |

+----+

3 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值