Мне стыдно, стыдно, стыдно, стыдно...
Привет.
Почему-то неправильно сортируется кириллица в mysql.
$ locale
LANG=ru_RU.utf8
LC_CTYPE="ru_RU.utf8"
LC_NUMERIC="ru_RU.utf8"
LC_TIME="ru_RU.utf8"
LC_COLLATE="ru_RU.utf8"
LC_MONETARY="ru_RU.utf8"
LC_MESSAGES="ru_RU.utf8"
LC_PAPER="ru_RU.utf8"
LC_NAME="ru_RU.utf8"
LC_ADDRESS="ru_RU.utf8"
LC_TELEPHONE="ru_RU.utf8"
LC_MEASUREMENT="ru_RU.utf8"
LC_IDENTIFICATION="ru_RU.utf8"
LC_ALL=
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 59
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database `sample` character set utf8;
Query OK, 1 row affected (0.01 sec)
mysql> show create database `sample`;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| sample | CREATE DATABASE `sample` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> use sample;
Database changed
mysql> create table `example` (`id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), PRIMARY KEY(`id`)) DEFAULT CHARACTER SET=utf8 COLLATE=`utf8_unicode_ci`;
Query OK, 0 rows affected (0.09 sec)
mysql> insert into `example` values ('', 'А');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'Б');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'В');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'Г');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'Д');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'Е');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'Ё');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'Ж');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into `example` values ('', 'З');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from example order by `name` asc;
+----+------+
| id | name |
+----+------+
| 1 | А |
| 7 | Ё |
| 8 | Ж |
| 9 | З |
| 2 | Б |
| 3 | В |
| 4 | Г |
| 5 | Д |
| 6 | Е |
+----+------+
9 rows in set (0.00 sec)
mysql> select * from example order by `name` desc;
+----+------+
| id | name |
+----+------+
| 6 | Е |
| 5 | Д |
| 4 | Г |
| 3 | В |
| 2 | Б |
| 9 | З |
| 8 | Ж |
| 1 | А |
| 7 | Ё |
+----+------+
9 rows in set (0.00 sec)
Почему так? Не то сопоставление? А какое будет верным для кириллицы?
mysql> show collation where `charset`='utf8';
+--------------------+---------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+--------------------+---------+-----+---------+----------+---------+
| utf8_general_ci | utf8 | 33 | Yes | Yes | 1 |
| utf8_bin | utf8 | 83 | | Yes | 1 |
| utf8_unicode_ci | utf8 | 192 | | Yes | 8 |
| utf8_icelandic_ci | utf8 | 193 | | Yes | 8 |
| utf8_latvian_ci | utf8 | 194 | | Yes | 8 |
| utf8_romanian_ci | utf8 | 195 | | Yes | 8 |
| utf8_slovenian_ci | utf8 | 196 | | Yes | 8 |
| utf8_polish_ci | utf8 | 197 | | Yes | 8 |
| utf8_estonian_ci | utf8 | 198 | | Yes | 8 |
| utf8_spanish_ci | utf8 | 199 | | Yes | 8 |
| utf8_swedish_ci | utf8 | 200 | | Yes | 8 |
| utf8_turkish_ci | utf8 | 201 | | Yes | 8 |
| utf8_czech_ci | utf8 | 202 | | Yes | 8 |
| utf8_danish_ci | utf8 | 203 | | Yes | 8 |
| utf8_lithuanian_ci | utf8 | 204 | | Yes | 8 |
| utf8_slovak_ci | utf8 | 205 | | Yes | 8 |
| utf8_spanish2_ci | utf8 | 206 | | Yes | 8 |
| utf8_roman_ci | utf8 | 207 | | Yes | 8 |
| utf8_persian_ci | utf8 | 208 | | Yes | 8 |
| utf8_esperanto_ci | utf8 | 209 | | Yes | 8 |
| utf8_hungarian_ci | utf8 | 210 | | Yes | 8 |
+--------------------+---------+-----+---------+----------+---------+
21 rows in set (0.00 sec)