0
的功能組成
考慮簡單的student表,歧義在MySQL
mysql> select * from student;
+--------------+--------------+-------+
| student_name | joining_date | marks |
+--------------+--------------+-------+
| Anurag | 2013-06-15 | 50 |
| Chandra | 2014-07-12 | 65 |
| Dev | 2014-03-25 | 80 |
| Gopal | 2015-05-12 | 60 |
| Indra | 2015-05-20 | 75 |
| Ram | 2015-01-10 | 75 |
| Shyam | 2015-01-10 | 50 |
+--------------+--------------+-------+
7 rows in set (0.00 sec)
我需要找到:
在哪一年,也最大的學生參加了學校?
所以,它類似於max(count())
與group by
。但由於SQL不允許max(count())
,我發現使用>= all
的替代方法。
下面是查詢和結果:
mysql> select extract(year from joining_date) from student group by extract(year from joining_date) having count(student_name) >= all(select count(student_name) from student group by joining_date);
+---------------------------------+
| extract(year from joining_date) |
+---------------------------------+
| 2014 |
| 2015 |
+---------------------------------+
2 rows in set (0.00 sec)
我一定要得到的只有2015年,但它會產生兩個元。 當我從>= all
中刪除=
時,我得到了所需的結果。但是最大限度必須超過所有元素的權利?
對類似問題的另一個查詢使用>=
。更換>=' by
當
mysql> select * from account;
+----------------+-------------+---------+
| account_number | branch_name | balance |
+----------------+-------------+---------+
| A101 | Downtown | 500 |
| A102 | Perryridge | 400 |
| A201 | Brighton | 900 |
| A215 | Mianus | 700 |
| A217 | Brighton | 750 |
| A222 | Redwood | 700 |
| A305 | Round Hill | 350 |
+----------------+-------------+---------+
7 rows in set (0.00 sec)
我需要確定具有最高的平均餘額
mysql> select branch_name from account group by branch_name having avg(balance) >= all(select avg(balance) from account group by branch_name);
+-------------+
| branch_name |
+-------------+
| Brighton |
+-------------+
1 row in set (0.00 sec)
但分支>」我得到的,
:考慮帳戶表
mysql> select branch_name from account group by branch_name having avg(balance) > all(select avg(balance) from account group by branch_name);
Empty set (0.00 sec)
這裏有什麼問題?
您已設置元素的'{A1:
返回所有以相等的最高平均分支,A2,... An}'根據定義,如果你選擇了最大集合,'max(Ai),i = 1..n',那麼它將仍然是一個相同集合的元素,因此它不能大於所有因素它不會屬於原始的集合。簡而言之 - 您的最大值仍然是原始集合的__some元素___,因此它將等於__at至少一個___元素。 –