如何在MySQL中控制UTF-8排序? [英] How can I control UTF-8 ordering in MySQL?

查看:54
本文介绍了如何在MySQL中控制UTF-8排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这是标准"SORT BY name"查询返回的顺序:

For example, this is the order that a standard 'SORT BY name' query is returning:

name
------
Cebbb
Čebbc
Cebbd

我想按名称排序,然后先得到重音字符 ,再与其他重音字符分组,例如

I'd like to SORT BY name and get the accented character first, grouped with other accented characters, e.g.

name
------
Čebbc
Cebbb
Cebbd

默认情况下,出于排序目的,MySql将Č视为 C ,并按显示的顺序进行排序.

By default, MySql treats Č as if it were C, for sorting purposes, and sorts in the order shown.

或者,在PHP中,有没有一种方法可以将Č转换为 C 以进行比较?

Alternatively, is there a way, in PHP, that I can 'convert' Č to C, for comparison purposes?

推荐答案

您可以在您的 ORDER BY 子句中添加一个 COLLATE 表达式:

You can add a COLLATE expression to your ORDER BY clause:

SELECT k
FROM t1
ORDER BY k COLLATE utf8_spanish_ci;

然后,将使用正确的排序规则对列进行排序,并且列定义可以保持不变.

Then, the column will be sorted using the correct collate, and the column definition can stay as it is.

这篇关于如何在MySQL中控制UTF-8排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆