ORDER BY UPPER(列)是错误的
ORDER BY UPPER (column) is wrong
使用 MySQL 5.6,给定以下 table 结构:
mysql> describe groups;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(500) | YES | | NULL | |
| description | varchar(200) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
以及
中的以下数据
`groups` table:
mysql> select id, name from groups;
+----+------------------+
| id | name |
+----+------------------+
| 1 | some-users |
| 2 | SOME-ADMINS |
| 3 | customers-group1 |
| 4 | customers-group2 |
| 5 | customers-group3 |
+----+------------------+
5 rows in set (0.00 sec)
以下查询在 MySQL 中排序不当(但它至少在 PostgreSQL、Oracle 和 MSSQL 中工作正常):
mysql> select id, name from groups order by upper(name);
+----+------------------+
| id | name |
+----+------------------+
| 3 | customers-group1 |
| 4 | customers-group2 |
| 5 | customers-group3 |
| 1 | some-users |
| 2 | SOME-ADMINS |
+----+------------------+
5 rows in set (0.00 sec)
我希望:
SOME-ADMINS
出现在 some-users
之前,其他数据库供应商也是如此。
这是 MySQL 的错误吗?
根据文档,它不区分大小写,但您可以使用 BINARY 使其区分大小写:
On character type columns, sorting—like all other comparison operations—is normally performed in a case-insensitive fashion. This means that the order is undefined for columns that are identical except for their case. You can force a case-sensitive sort for a column by using BINARY like so: ORDER BY BINARY col_name.
使用 MySQL 5.6,给定以下 table 结构:
mysql> describe groups;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(500) | YES | | NULL | |
| description | varchar(200) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
以及
中的以下数据`groups` table:
mysql> select id, name from groups;
+----+------------------+
| id | name |
+----+------------------+
| 1 | some-users |
| 2 | SOME-ADMINS |
| 3 | customers-group1 |
| 4 | customers-group2 |
| 5 | customers-group3 |
+----+------------------+
5 rows in set (0.00 sec)
以下查询在 MySQL 中排序不当(但它至少在 PostgreSQL、Oracle 和 MSSQL 中工作正常):
mysql> select id, name from groups order by upper(name);
+----+------------------+
| id | name |
+----+------------------+
| 3 | customers-group1 |
| 4 | customers-group2 |
| 5 | customers-group3 |
| 1 | some-users |
| 2 | SOME-ADMINS |
+----+------------------+
5 rows in set (0.00 sec)
我希望:
SOME-ADMINS
出现在 some-users
之前,其他数据库供应商也是如此。
这是 MySQL 的错误吗?
根据文档,它不区分大小写,但您可以使用 BINARY 使其区分大小写:
On character type columns, sorting—like all other comparison operations—is normally performed in a case-insensitive fashion. This means that the order is undefined for columns that are identical except for their case. You can force a case-sensitive sort for a column by using BINARY like so: ORDER BY BINARY col_name.