mysql> explain select * from user group by name,age \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: user
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 36
Extra: Using temporary; Using filesort
1 row in set (0.00 sec)
mysql> explain select * from user group by name,age order by null \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: user
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 36
Extra: Using temporary
1 row in set (0.00 sec)
注意有下划线的红色字体,是没有加‘order by null’出现的提示,表示进行了排序,以下是结果:
mysql> select * from user group by name,age order by null
+-------------+------+----+
| name | age | id |
+-------------+------+----+
| zhangzhibin | 13 | 1 |
| lisi | 13 | 2 |
| wangwu | 18 | 3 |
| zhangzhibin | 19 | 4 |
| lisi | 12 | 5 |
| wangwu | 13 | 6 |
| zhangzhibin | 11 | 7 |
| lisi | 14 | 8 |
| wangwu | 16 | 9 |
| zhangzhibin | 18 | 10 |
| wangwu | 11 | 12 |
| lisi | 15 | 14 |
| wangwu | 10 | 15 |
| lisi | 18 | 17 |
| wangwu | 17 | 18 |
| zhangzhibin | 12 | 19 |
| lisi | 10 | 20 |
| lisi | 19 | 23 |
| wangwu | 14 | 24 |
| lisi | 11 | 26 |
| zhangzhibin | 17 | 28 |
| wangwu | 15 | 30 |
| zhangzhibin | 14 | 34 |
+-------------+------+----+
23 rows in set (0.00 sec)
mysql> select * from user group by name,age;
+-------------+------+----+
| name | age | id |
+-------------+------+----+
| lisi | 10 | 20 |
| lisi | 11 | 26 |
| lisi | 12 | 5 |
| lisi | 13 | 2 |
| lisi | 14 | 8 |
| lisi | 15 | 14 |
| lisi | 18 | 17 |
| lisi | 19 | 23 |
| wangwu | 10 | 15 |
| wangwu | 11 | 12 |
| wangwu | 13 | 6 |
| wangwu | 14 | 24 |
| wangwu | 15 | 30 |
| wangwu | 16 | 9 |
| wangwu | 17 | 18 |
| wangwu | 18 | 3 |
| zhangzhibin | 11 | 7 |
| zhangzhibin | 12 | 19 |
| zhangzhibin | 13 | 1 |
| zhangzhibin | 14 | 34 |
| zhangzhibin | 17 | 28 |
| zhangzhibin | 18 | 10 |
| zhangzhibin | 19 | 4 |
+-------------+------+----+
23 rows in set (0.00 sec)
为了显示出order by null 这句的作用我选择了 name 和 age 两个字段进行分组,若只选name来进行分组,那么最后的结果只有三条,不容易看出结果