CONVERT_TO
Descriptionβ
Converts the character encoding of a column to a specified target character set. This function is typically used in the ORDER BY clause to ensure that columns containing Chinese characters are sorted according to their pinyin order. Currently, only conversion to 'gbk'
is supported.
Syntaxβ
CONVERT_TO(<column>, <character>)
Parametersβ
Parameters | Description |
---|---|
<column> | The VARCHAR column whose encoding is to be converted. |
<character> | The target character set. Currently, only 'gbk' is supported. |
Return Valueβ
Returns a VARCHAR value with the converted encoding, allowing proper pinyin-based ordering when used in the ORDER BY clause.
Examplesβ
SELECT * FROM class_test ORDER BY class_name;
+----------+------------+-------------+
| class_id | class_name | student_ids |
+----------+------------+-------------+
| 6 | asd | [6] |
| 7 | qwe | [7] |
| 8 | z | [8] |
| 2 | ε | [2] |
| 3 | ε¦ | [3] |
| 1 | ε | [1] |
| 4 | εΌ | [4] |
| 5 | ζ | [5] |
+----------+------------+-------------+
SELECT * FROM class_test ORDER BY CONVERT_TO(class_name, 'gbk');
+----------+------------+-------------+
| class_id | class_name | student_ids |
+----------+------------+-------------+
| 6 | asd | [6] |
| 7 | qwe | [7] |
| 8 | z | [8] |
| 1 | ε | [1] |
| 2 | ε | [2] |
| 3 | ε¦ | [3] |
| 5 | ζ | [5] |
| 4 | εΌ | [4] |
+----------+------------+-------------+