MySQL的max函数和min函数:

在文本中的应用,中文与英文排序是不同的。

先用中文来一遍看看(这里为了快速展示,关键字没有大写,不太符合规范):

 1 mysql> select countryname
 2     -> from country
 3     -> order by countryname;
 4 +-------------+
 5 | countryname |
 6 +-------------+
 7 | 中国        |
 8 | 俄罗斯      |
 9 | 哈利哈      |
10 | 哈利希      |
11 | 法国        |
12 | 美国        |
13 | 英国        |
14 +-------------+
15 7 rows in set (0.00 sec)
1 mysql> select max(countryname) as cn
2     -> from country;
3 +--------+
4 | cn     |
5 +--------+
6 | 英国   |
7 +--------+
8 1 row in set (0.00 sec)
1 mysql> select min(countryname) as cn
2     -> from country;
3 +--------+
4 | cn     |
5 +--------+
6 | 中国   |
7 +--------+
8 1 row in set (0.00 sec)

 英文的话,是按照字母表顺序,而非ASCII码来的,在ASCII码表中,65~90为26个大写英文字母,97~122号为26个小写英文字母,h的ASCII码显然在R后面,但是排序却在前面,显然很奇怪。下面是我自己的机子运行的来,若有错,望指正:

 1 mysql> select countrycode
 2     -> from country
 3     -> order by countrycode;
 4 +-------------+
 5 | countrycode |
 6 +-------------+
 7 | CN          |
 8 | FR          |
 9 | GB          |
10 | hlh         |
11 | hlx         |
12 | RU          |
13 | US          |
14 +-------------+
15 7 rows in set (0.00 sec)
1 mysql> select min(countrycode) as cc
2     -> from country;
3 +------+
4 | cc   |
5 +------+
6 | CN   |
7 +------+
8 1 row in set (0.00 sec)
1 mysql> select max(countrycode) as cc
2     -> from country;
3 +------+
4 | cc   |
5 +------+
6 | US   |
7 +------+
8 1 row in set (0.00 sec)

 下面看看一下大写小写英文字母谁前谁后:

 1 mysql> select * from country;
 2 +----+-------------+-------------+
 3 | id | countryname | countrycode |
 4 +----+-------------+-------------+
 5 |  1 | 中国        | CN          |
 6 |  2 | 美国        | US          |
 7 |  3 | 俄罗斯      | RU          |
 8 |  4 | 英国        | GB          |
 9 |  5 | 法国        | FR          |
10 |  6 | 哈利哈      | hlh         |
11 |  7 | 哈利希      | hlx         |
12 +----+-------------+-------------+
13 7 rows in set (0.00 sec)
14
15 mysql> insert into country
16     -> (id,countryname,countrycode)
17     -> values (8,"哈尼","Hn");
18 Query OK, 1 row affected (0.10 sec)
 1 mysql> select countrycode
 2     -> from country
 3     -> order by countrycode;
 4 +-------------+
 5 | countrycode |
 6 +-------------+
 7 | CN          |
 8 | FR          |
 9 | GB          |
10 | hlh         |
11 | hlx         |
12 | Hn          |
13 | RU          |
14 | US          |
15 +-------------+
16 8 rows in set (0.00 sec)

看起来是大写排在后面了。

01-04 08:26