Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 294.0Kb
Man Pages (Zip) - 409.0Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb
Excerpts from this Manual

MySQL 8.3 Reference Manual  /  ...  /  The Row Holding the Maximum of a Certain Column

5.6.2 某一特定列的最大行

任务:查找最昂贵的文章的编号、经销商和价格。

这可以使用子查询轻松实现:

SELECT article, dealer, price
FROM   shop
WHERE  price=(SELECT MAX(price) FROM shop);

+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0004 | D      | 19.95 |
+---------+--------+-------+

另一种解决方案是使用 LEFT JOIN,如下所示:

SELECT s1.article, s1.dealer, s1.price
FROM shop s1
LEFT JOIN shop s2 ON s1.price < s2.price
WHERE s2.article IS NULL;

您也可以通过对价格进行降序排序,然后使用 MySQL 特有的 LIMIT 子句获取第一行,如下所示:

SELECT article, dealer, price
FROM shop
ORDER BY price DESC
LIMIT 1;
Note

如果有多篇最昂贵的文章,每篇价格都是 19.95,那么 LIMIT 解决方案将只显示其中一篇。