排序规则(即 COLLATE 运算符)用于指定字符序,可以用于重新设定字符类型的字符序或为单个表达式指定字符序。
COLLATE 是后缀一元运算符,与其它一元运算符优先级相同,但是在所有前缀一元运算符之后执行。COLLATE 运算符可以应用于 VARCHAR2,CHAR,LONG,NVARCHAR 或 NCHAR 类型的表达式。
COLLATE 运算符后跟一个参数 collation_name,表示需要设置或使用的字符序,字符序名称包含空格时必须在名称外加双引号。
举例如下:
obclient> create table t1(c1 varchar(20));
obclient> insert into t1 values('a');
obclient> insert into t1 values('b');
obclient> insert into t1 values(1);
obclient> select * from t1 order by c1 collate utf8mb4_general_ci;
+------+
| C1 |
+------+
| 1 |
| a |
| b |
+------+
3 rows in set (0.00 sec)
目前支持如下排序规则:
obclient> show collation;
+--------------------+---------+-----+---------+----------+---------+
| COLLATION | CHARSET | ID | DEFAULT | COMPILED | SORTLEN |
+--------------------+---------+-----+---------+----------+---------+
| utf8mb4_general_ci | utf8mb4 | 45 | Yes | Yes | 1 |
| utf8mb4_bin | utf8mb4 | 46 | NULL | Yes | 1 |
| binary | binary | 63 | Yes | Yes | 1 |
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 |
| gbk_bin | gbk | 87 | NULL | Yes | 1 |
| utf16_general_ci | utf16 | 54 | Yes | Yes | 1 |
| utf16_bin | utf16 | 55 | NULL | Yes | 1 |
| utf8mb4_unicode_ci | utf8mb4 | 224 | NULL | Yes | 1 |
| utf16_unicode_ci | utf16 | 101 | NULL | Yes | 1 |
| gb18030_chinese_ci | gb18030 | 248 | Yes | Yes | 1 |
| gb18030_bin | gb18030 | 249 | NULL | Yes | 1 |
+--------------------+---------+-----+---------+----------+---------+
11 rows in set (0.01 sec)