这是MySQL排序错误吗?

时间:2011-04-22 14:57:01

标签: mysql sorting

我遇到了奇怪的服务器行为MySQL 5.1.50。它错误地对记录进行排序。

例如,我创建了一个表test

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;


INSERT INTO `test` (`id`, `title`) VALUES
(1, 'record1'),
(2, 'record2'),
(3, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
(4, 'ABCDEFGHIJKLMNOPQRSTUVWXYY');

并进行查询:

的MySQL>设置名称utf8; 查询正常,0行受影响(0.00秒)

的MySQL>按标题asc从测试顺序中选择*;

+----+----------------------------+
| id | title                      |
+----+----------------------------+
|  3 | ABCDEFGHIJKLMNOPQRSTUVWXYZ |
|  4 | ABCDEFGHIJKLMNOPQRSTUVWXYY |
|  1 | record1                    |
|  2 | record2                    |
+----+----------------------------+

4行(0.00秒)

的MySQL>按标题desc从测试顺序中选择*;

+----+----------------------------+
| id | title                      |
+----+----------------------------+
|  2 | record2                    |
|  1 | record1                    |
|  3 | ABCDEFGHIJKLMNOPQRSTUVWXYZ |
|  4 | ABCDEFGHIJKLMNOPQRSTUVWXYY |
+----+----------------------------+

4行(0.00秒)

如您所见,记录3和4不会改变位置

我做了这样的查询,有些字母不会改变订单,例如A和a。

的MySQL> SELECT * FROM test ORDER BY title COLLATE utf8_unicode_ci ASC;

+----+---------+
| id | title   |
+----+---------+
|  1 | A       |
| 27 | a       |
| 28 | b       |
|  2 | B       |
| 29 | c       |
|  3 | C       |
|  4 | D       |
| 30 | d       |
| 31 | e       |
|  5 | E       |
|  6 | F       |
| 32 | f       |
| 33 | g       |
|  7 | G       |
| 34 | h       |
|  8 | H       |
| 35 | i       |
|  9 | I       |
| 36 | j       |
| 10 | J       |
| 11 | K       |
| 37 | k       |
| 12 | L       |
| 38 | l       |
| 39 | m       |
| 13 | M       |
| 40 | n       |
| 14 | N       |
| 41 | o       |
| 15 | O       |
| 42 | p       |
| 16 | P       |
| 17 | Q       |
| 43 | q       |
| 44 | r       |
| 18 | R       |
| 19 | S       |
| 45 | s       |
| 20 | T       |
| 46 | t       |
| 21 | U       |
| 47 | u       |
| 48 | v       |
| 22 | V       |
| 49 | w       |
| 23 | W       |
| 50 | x       |
| 24 | X       |
| 25 | Y       |
| 51 | y       |
| 26 | Z       |
| 52 | z       |
+----+---------+

的MySQL> SELECT * FROM test ORDER BY title COLLATE utf8_unicode_ci DESC;

+----+---------+
| id | title   |
+----+---------+
| 52 | z       |
| 26 | Z       |
| 25 | Y       |
| 51 | y       |
| 50 | x       |
| 24 | X       |
| 49 | w       |
| 23 | W       |
| 48 | v       |
| 22 | V       |
| 47 | u       |
| 21 | U       |
| 20 | T       |
| 46 | t       |
| 45 | s       |
| 19 | S       |
| 18 | R       |
| 44 | r       |
| 17 | Q       |
| 43 | q       |
| 16 | P       |
| 42 | p       |
| 41 | o       |
| 15 | O       |
| 40 | n       |
| 14 | N       |
| 39 | m       |
| 13 | M       |
| 12 | L       |
| 38 | l       |
| 11 | K       |
| 37 | k       |
| 10 | J       |
| 36 | j       |
|  9 | I       |
| 35 | i       |
|  8 | H       |
| 34 | h       |
|  7 | G       |
| 33 | g       |
| 32 | f       |
|  6 | F       |
|  5 | E       |
| 31 | e       |
|  4 | D       |
| 30 | d       |
| 29 | c       |
|  3 | C       |
|  2 | B       |
| 28 | b       |
|  1 | A       |
| 27 | a       |
+----+---------+

我认为这是整理的错误。

可能有人遇到过这样的服务器行为吗?

3 个答案:

答案 0 :(得分:1)

确实是您的版本中的错误。

我尝试使用MySQL 5.5.8并在那里正确排序:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.8 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select * from test order by title asc;
+----+----------------------------+
| id | title                      |
+----+----------------------------+
|  4 | ABCDEFGHIJKLMNOPQRSTUVWXYY |
|  3 | ABCDEFGHIJKLMNOPQRSTUVWXYZ |
|  1 | record1                    |
|  2 | record2                    |
+----+----------------------------+
4 rows in set (0.00 sec)

mysql> select * from test order by title desc;
+----+----------------------------+
| id | title                      |
+----+----------------------------+
|  2 | record2                    |
|  1 | record1                    |
|  3 | ABCDEFGHIJKLMNOPQRSTUVWXYZ |
|  4 | ABCDEFGHIJKLMNOPQRSTUVWXYY |
+----+----------------------------+
4 rows in set (0.00 sec)

mysql>

答案 1 :(得分:1)

如果你尝试会怎么样?:

SELECT *
FROM test
ORDER BY title ASC
COLLATE utf8_unicode_ci;

你试过这个:

INSERT INTO test (id, title)
VALUES
(101, 'A'),
(102, 'B'),
(103, 'C'),
...
(126, 'Z');

SELECT *
FROM test
ORDER BY title ASC
;

查看问题(或错误)是否与长字符串或整理有关?

答案 2 :(得分:0)

由于您有文本和数值的混合,您可能会尝试这样排序:

mysql> select * from test order by length(title), title;

here所述。

[如果这不起作用,您可以根据需要用ascdesc替换第二个标题实例。