這裏探討了分組查詢最大值(group-wise-max)的問題。涉及到 SQL 查詢語句中的 GROUP BY 子句及鏈接(JOIN)操做。html
本文緣起於 SegmentFault上 的一個問題:
http://segmentfault.com/q/1010000004138670mysql
下面是提問者的表和測試數據:sql
create table test ( id smallint unsigned not null auto_increment, name varchar(20) not null, age smallint unsigned not null, class smallint unsigned not null, primary key (id)); insert into test (name, age, class) values ('wang', 11, 3), ('qiu', 22, 1), ('liu', 42, 1), ('qian', 20, 2), ('zheng', 20, 2), ('li', 33, 3);
能夠理解成學生信息,簡單的 SELECT 一下:數據庫
mysql> select * from test; +----+-------+-----+-------+ | id | name | age | class | +----+-------+-----+-------+ | 1 | wang | 11 | 3 | | 2 | qiu | 22 | 1 | | 3 | liu | 42 | 1 | | 4 | qian | 20 | 2 | | 5 | zheng | 20 | 2 | | 6 | li | 33 | 3 | +----+-------+-----+-------+
問題:如何選出每班中年齡最大者?segmentfault
使用 GROUP BY 子句,這一點毫無疑問。函數
select class, max(age) from test group by class;
+-------+----------+ | class | max(age) | +-------+----------+ | 1 | 42 | | 2 | 20 | | 3 | 33 | +-------+----------+
結果按 class
分組了,最大年齡也選出來了,可是沒有 id
和 name
。測試
添加其它列到 SELECT 子句。this
select id, name, max(age), class from test group by class;
+----+------+----------+-------+ | id | name | max(age) | class | +----+------+----------+-------+ | 2 | qiu | 42 | 1 | | 4 | qian | 20 | 2 | | 1 | wang | 33 | 3 | +----+------+----------+-------+
結果並不正確,各列發生"錯位",年齡 42 的應該是 liu 而不是 qiu,緣由是它違反了下面這條規則:code
包含 GROUP BY 的 SQL 語句,被 select 的列要麼使用聚合函數,要麼出如今GROUP BY 子句中。htm
上面的 SELECT 語句,id
,name
沒有出如今 GROUP BY 子句,也沒有使用聚合函數,因此它違反了規則,不是一條正確的SQL語句。
select t1.* from test t1, (select class, max(age) as age from test group by class) t2 where t1.class = t2.class and t1.age = t2.age;
+----+-------+-----+-------+ | id | name | age | class | +----+-------+-----+-------+ | 3 | liu | 42 | 1 | | 5 | zheng | 22 | 2 | | 6 | li | 33 | 3 | +----+-------+-----+-------+
結果正確。
這條語句引用了兩個表(t1
和 t2
),語義上至關於內鏈接(INNER JOIN)。
使用內鏈接改寫上面那條語句。
注意:關鍵字 JOIN,就是指 INNER JOIN。固然你也能夠顯式地寫成 INNER JOIN。
select t1.* from test t1 join ( select class, max(age) as age from test group by class) t2 on t1.class = t2.class and t1.age = t2.age;
+----+-------+-----+-------+ | id | name | age | class | +----+-------+-----+-------+ | 3 | liu | 42 | 1 | | 5 | zheng | 22 | 2 | | 6 | li | 33 | 3 | +----+-------+-----+-------+
使用左鏈接(LEFT JOIN)來實現。沒有用到 GROUP BY。
select t1.* from test t1 left join test t2 on t1.class = t2.class and t1.age < t2.age where t2.class is null;
根據定義,左鏈接會從左表那裏返回全部的行,即便在右表中沒有匹配的行。
這條語句參考自:The Rows Holding the Group-wise Maximum of a Certain Column
原理在此:JOIN Syntax
摘錄以下:
If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another table:
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;
This example finds all rows in left_tbl with an id value that is not present in right_tbl (that is, all rows in left_tbl with no corresponding row in right_tbl). This assumes that right_tbl.id is declared NOT NULL.
可見,這條語句中的 WHERE 子句,其實能夠用任何列做爲條件。下面這句也是同樣的效果:
select t1.* from test t1 left join test t2 on t1.class = t2.class and t1.age < t2.age where t2.id is null;
把 zheng 的年齡改成 20,那麼 class 2 中,qian 和 zheng 的年齡都是最大值 20。
update test set age=20 where name='zheng';
如今執行如上查詢,結果爲:
+----+-------+-----+-------+ | id | name | age | class | +----+-------+-----+-------+ | 3 | liu | 42 | 1 | | 4 | qian | 20 | 2 | | 5 | zheng | 20 | 2 | | 6 | li | 33 | 3 | +----+-------+-----+-------+
使用內鏈接和左鏈接的兩條語句,執行結果保持一致,都能顯示出各組最大值的多行記錄。
GROUP BY 子句將表按列的值分組,列的值相同的分在一組。若是 GROUP BY 後有多個列名,則先按第一列名分組,再按第二列名在組中分組,原則上能夠一直分下去,直到在全部基本組中,GROUP BY 子句所指定的列都具備相同的值,HAVING 後的條件是選擇基本組的條件。GROUP BY 子句常與彙集函數聯用,此時彙集函數以基本組爲計算對象。加了 GROUP BY 子句後,SELECT 子句所取的值必須在基本組中是惟一的,即只能是 GROUP BY 子句所指明的列或彙集函數。若無 GROUP BY 子句,則彙集函數以整個表爲計算對象,此時 SELECT 子句只能取彙集函數,而不能取某一列。
王能斌,《數據庫系統教程》(第二版),3.4.3。