讓我們了解如何在MySQL 中尋找保存特定列的分組最大值的行-
尋找保存分組最大值的行的語法MySQL 中特定列的最大值如下-
SELECT colName1, colName2, colName3 FROM tableName s1 WHERE colName3=(SELECT MAX(s2. colName3) FROM tableName s2 WHERE s1. colName1= s2. colName1) ORDER BY colName1;
假設我們有以下產品表-
+---------+----------+--------+ | Article | Warehouse| Price | +---------+----------+--------+ | 1 | North | 255.50 | | 1 | North | 256.05 | | 2 | South | 90.50 | | 3 | East | 120.50 | | 3 | East | 123.10 | | 3 | East | 122.10 | +---------+----------+--------|
以下是查詢−
################################################# #SELECT Article, Warehouse, Price FROM Product p1 WHERE Price=(SELECT MAX(p2. Price) FROM Product p2 WHERE p1. Article= p2. Article) ORDER BY Article;###輸出###
+-------------+----------------+------------+ | Article | Warehouse | Price | +-------------+----------------+------------+ | 0001 | North | 256.05 | | 0002 | South | 90.50 | | 0003 | East | 123.10 | +-------------+----------------+------------+###上面的查詢使用了相關子查詢。 ###
以上是MySQL 中保存某一列的分組最大值的行的詳細內容。更多資訊請關注PHP中文網其他相關文章!