MySQL無GROUP BY直接HAVING前往空的成績剖析。本站提示廣大學習愛好者:(MySQL無GROUP BY直接HAVING前往空的成績剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL無GROUP BY直接HAVING前往空的成績剖析正文
有一張表,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之前,必定要弄清晰它的行動能否與懂得的分歧。