MySQL 中包含某列按组最大值的那些行
让我们了解如何在 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;
我们假设我们有以下 PRODUCT 表 -
<PRODUCT>
+---------+----------+--------+ | 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 | +-------------+----------------+------------+
以上查询使用了相关子查询。
广告