2015-06-23 32 views
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) 

這裏有什麼問題?

+0

您已設置元素的'{A1:

SELECT branch_name FROM account GROUP BY branch_name ORDER BY AVG(balance) DESC LIMIT 1; 

返回所有以相等的最高平均分支,A2,... An}'根據定義,如果你選擇了最大集合,'max(Ai),i = 1..n',那麼它將仍然是一個相同集合的元素,因此它不能大於所有因素它不會屬於原始的集合。簡而言之 - 您的最大值仍然是原始集合的__some元素___,因此它將等於__at至少一個___元素。 –

回答

0

最大的學生

只返回第一個找到的一年,最大的學生:

SELECT tmp.year 
FROM (
    SELECT count(*) AS total, EXTRACT(year FROM joining_date) AS year 
    FROM student 
    GROUP BY year 
    ORDER BY total DESC 
) AS tmp 
LIMIT 1; 

返回所有年,等於最大的學生:

SELECT tmp.year 
FROM (
    SELECT count(*) AS total, EXTRACT(year FROM joining_date) AS year 
    FROM student 
    GROUP BY year 
) AS tmp 
WHERE tmp.total = (
    SELECT MAX(tmp.total) 
    FROM (
     SELECT count(*) AS total 
     FROM student 
     GROUP BY EXTRACT(year FROM joining_date) 
    ) AS tmp 
); 

我尋找方法來優化這通過重用子查詢,但沒有發現任何東西沒有使它變得更大。

最高平均餘額

只返回與最高平均第一個找到的分支:

SELECT branch_name 
FROM account 
GROUP BY branch_name 
HAVING AVG(balance) = (
    SELECT AVG(balance) 
    FROM account 
    GROUP BY branch_name 
    ORDER BY AVG(balance) DESC 
    LIMIT 1 
);