3.6.2 The Row Holding the Maximum of a Certain Column包含某一列的最大值的行

Task: Find the number, dealer, and price of the most expensive article.任务:找到最昂贵物品的编号、经销商和价格。

This is easily done with a subquery:这可以通过子查询轻松完成:

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

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

Other solutions are to use a LEFT JOIN or to sort all rows descending by price and get only the first row using the MySQL-specific LIMIT clause:其他解决方案是使用LEFT JOIN或按价格降序排列所有行,并使用MySQL特定的LIMIT子句仅获取第一行:

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

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

If there were several most expensive articles, each with a price of 19.95, the LIMIT solution would show only one of them.如果有几个最昂贵的物品,每个物品的价格为19.95,LIMIT解决方案将只显示其中一个。