在下面的例子中,为什么min()
查询返回结果,但是max()
查询没有?
mysql> create table t(id int, a int);
Query OK, 0 rows affected (0.10 sec)
mysql> insert into t(id, a) values(1, 1);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t(id, a) values(1, 2);
Query OK, 1 row affected (0.02 sec)
mysql> select * from t
-> ;
+------+------+
| id | a |
+------+------+
| 1 | 1 |
| 1 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from t where a < 4;
+------+------+
| id | a |
+------+------+
| 1 | 1 |
| 1 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from t where a < 4 having a = max(a);
Empty set (0.00 sec)
mysql> select * from t where a < 4 having a = min(a);
+------+------+
| id | a |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
The HAVING
子句用于过滤行组。你参考一下min(a)
and max(a)
其中(在没有任何GROUP BY
子句)总计a
表中的值,然后与单个值进行比较a
value.
那么哪个a
MySQL应该使用什么值?我所知道的所有其他 RDBMS 都会在此时抛出错误,但 MySQL 确实允许这样做。来自文档 http://dev.mysql.com/doc/refman/5.6/en/group-by-hidden-columns.html
标准 SQL 不允许HAVING
命名任何列的子句
没有找到在GROUP BY
子句,除非它包含在聚合中
功能。 MySQL 允许使用此类列来简化
计算。此扩展假设未分组的列将
具有相同的分组值。否则,结果是
不定。
因此,从您得到的结果来看,它最终使用了1
作为标量值a
但这种行为并不能得到保证,它同样可以使用2
或任何其他现有的a
value.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)