当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL无GROUP BY直接HAVING返回空的问题分析

MySQL无GROUP BY直接HAVING返回空的问题分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

有一张表,id是主键,这样的写法可以返回一条记录:

复制代码 代码如下:

“select * from t having id=min(id);”

但是只是把min换成max,这样返回就是空了:
复制代码 代码如下:

“select * from t having id=max(id);”

这是为什么呢?
我们先来做个试验,验证这种情况。
这是表结构,初始化两条记录,然后试验:

复制代码 代码如下:

root@localhost : plx 10:25:10> show create table t2\g
*************************** 1. row ***************************
       table: t2
create table: create table `t2` (
  `a` int(11) default null,
  `id` int(10) unsigned not null auto_increment,
  primary key (`id`)
) engine=innodb auto_increment=5 default charset=utf8

root@localhost : plx 10:25:15> select * from t2;
+------+----+
| a    | id |
+------+----+
|    1 |  1 |
|    1 |  3 |
+------+----+
2 rows in set (0.00 sec)

root@localhost : plx 10:25:20> select * from t2 having id=min(id);
+------+----+
| a    | id |
+------+----+
|    1 |  1 |
+------+----+
1 row in set (0.00 sec)

root@localhost : plx 10:25:30> select * from t2 having id=max(id);
empty set (0.00 sec)

初看之下,好像真的是这样哎,怎么会这样呢?我再试一下,把a字段改一个为10,然后试下a字段:

复制代码 代码如下:

root@localhost : plx 10:26:58> select * from t2;
+------+----+
| a    | id |
+------+----+
|   10 |  1 |
|    1 |  3 |
+------+----+
2 rows in set (0.00 sec)

root@localhost : plx 10:28:20> select * from t2 having a=max(a);
+------+----+
| a    | id |
+------+----+
|   10 |  1 |
+------+----+
1 row in set (0.00 sec)

root@localhost : plx 10:28:28> select * from t2 having a=min(a);
empty set (0.00 sec)

我擦,这回max能返回,min不能了,这又是为啥呢?

旁白
一般来说,having子句是配合group by使用的,单独使用having本身是不符合规范的,
但是mysql会做一个重写,加上一个group by null,”select * from t having id=min(id)”会被重写为”select * from t group by null having id=min(id)”,这样语法就符合规范了。
继续……
但是,这个 group by null 会产生什么结果呢?经过查看代码和试验,可以证明,group by null 等价于 limit 1:

复制代码 代码如下:

root@localhost : plx 10:25:48> select * from t2 group by null;
+------+----+
| a    | id |
+------+----+
|   10 |  1 |
+------+----+
1 row in set (0.00 sec)

也就是说,group by null 以后,只会有一个分组,里面就是第一行数据。
但是如果这样,min、max结果应该是一致的,那也不应该max和min一个有结果,一个没结果啊,这是为什么呢,再做一个测试。
修改一下数据,然后直接查看min/max的值:

复制代码 代码如下:

root@localhost : plx 10:26:58> select * from t2;
+------+----+
| a    | id |
+------+----+
|   10 |  1 |
|    1 |  3 |
+------+----+
2 rows in set (0.00 sec)

root@localhost : plx 10:27:04> select * from t2 group by null;
+------+----+
| a    | id |
+------+----+
|   10 |  1 |
+------+----+
1 row in set (0.00 sec)

root@localhost : plx 10:30:21> select max(a),min(a),max(id),min(id) from t2 group by null;
+--------+--------+---------+---------+
| max(a) | min(a) | max(id) | min(id) |
+--------+--------+---------+---------+
|     10 |      1 |       3 |       1 |
+--------+--------+---------+---------+
1 row in set (0.00 sec)

是不是发现问题了?
max/min函数取值是全局的,而不是limit 1这个分组内的。
因此,当group by null的时候,max/min函数是取所有数据里的最大和最小值!
所以啊,”select * from t having id=min(id)”本质上是”select * from t having id=1″, 就能返回一条记录,而”select * from t having id=max(id)”本质上是”select * from t having id=3″,当然没有返回记录,这就是问题的根源。
测试一下group by a,这样就对了,每个分组内只有一行,所以max/min一样大,这回是取得组内最大和最小值。

复制代码 代码如下:

root@localhost : plx 11:29:49> select max(a),min(a),max(id),min(id) from t2 group by a;
+--------+--------+---------+---------+
| max(a) | min(a) | max(id) | min(id) |
+--------+--------+---------+---------+
|      1 |      1 |       3 |       3 |
|     10 |     10 |       5 |       5 |
+--------+--------+---------+---------+
2 rows in set (0.00 sec)

group by null时max/min的行为,是这个问题的本质,所以啊,尽量使用标准语法,玩花样sql之前,一定要搞清楚它的行为是否与理解的一致。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网