[英]What collation does MySQL use by default for ORDER BY?
These queries both give the result I expect: 这些查询都给出了我期望的结果:
SELECT sex
FROM ponies
ORDER BY sex COLLATE latin1_swedish_ci ASC
SELECT sex
FROM ponies
ORDER BY CONVERT(sex USING utf8) COLLATE utf8_general_ci ASC
| f |
| f |
| m |
| m |
+---+
But this query gives a different result: 但是此查询给出了不同的结果:
SELECT sex FROM ponies ORDER BY sex ASC
| m |
| m |
| f |
| f |
+---+
Here's the configuration: 配置如下:
SHOW VARIABLES LIKE 'collation\_%'
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
The table collation is latin1_swedish_ci
. 表排序规则为latin1_swedish_ci
。
MySQL server is 5.5.16. MySQL服务器是5.5.16。
Collation defaults are stored on a table-by-table basis. 排序规则默认值是逐表存储的。 There is a server-set default, but that is applied to the table at the time it is created. 有一个服务器设置的默认值,但是它在创建表时应用于表。
To find the collation for a specific table, run this query: 要查找特定表的排序规则,请运行以下查询:
SHOW TABLE STATUS LIKE 'ponies'\G
You should see output like this: 您应该看到如下输出:
*************************** 1. row ***************************
Name: ponies
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 8
Avg_row_length: 20
Data_length: 160
Max_data_length: 5629499534213119
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2012-02-27 10:16:25
Update_time: 2012-02-27 10:17:40
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
And you can see the Collation
setting in that result. 您可以在该结果中看到“ Collation
设置。
You can also override collation settings on particular columns within a table. 您还可以覆盖表中特定列的排序规则设置。 A create table statement like this would create a latin1_swedish_ci
table, with a utf8_polish_ci
column: CREATE TABLE语句像这样将创建一个latin1_swedish_ci
表,用utf8_polish_ci
列:
CREATE TABLE ponies (
sex CHAR(1) COLLATE utf8_polish_ci
) CHARACTER SET latin1 COLLATE latin1_swedish_ci;
The best way to view the results of this is like this: 查看此结果的最佳方法是这样的:
SHOW FULL COLUMNS FROM ponies;
Output: 输出:
+-------+---------+----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+---------+----------------+------+-----+---------+-------+---------------------------------+---------+
| sex | char(1) | utf8_polish_ci | YES | | NULL | | select,insert,update,references | |
+-------+---------+----------------+------+-----+---------+-------+---------------------------------+---------+
1 row in set (0.00 sec)
The documentation says it uses a case insensitive character comparison by default. 该文档说,默认情况下它使用不区分大小写的字符比较。 I don't see why you are not getting that result though. 我不明白为什么你没有得到那个结果。
The documentation also suggests using the binary
qualifier for case sensitive comparison. 该文档还建议使用binary
限定符进行区分大小写的比较。 I wonder if that would affect your result?: 我想知道这是否会影响您的结果?:
SELECT sex FROM ponies ORDER BY BINARY sex ASC
This behaviour can be observed when sex
is an ENUM
in which case it is usually sorted by the numerical position in the ENUM
definition. 当sex
是一个ENUM
时,可以观察到此行为,在这种情况下,通常按ENUM
定义中的数字位置对其进行排序。 Only when a collation is explicitly given an it is sorted in alphabetical order. 仅当明确指定排序规则时,它才按字母顺序排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.