当我遇到这种情况时,我正在练习一些 SQL。我想看看某种商品出现了多少次,然后从那里得到出现次数最多的商品。
这显示了每种商品出现了多少次:
mysql> SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count;
+----------------------+------------+
| commodity | count |
+----------------------+------------+
| PERSIAN MELON | 4 |
| BEANS | 6 |
| CASABA | 10 |
| ASPARAGUS | 11 |
| EGGPLANT | 12 |
| TOMATOES, CHERRY | 16 |
| GALIA MELON | 18 |
+-----------------------------------+
我试图获得最高的行,但这都是错误的:
mysql> SELECT commodity, MAX(COUNT(commodity)) count FROM orders GROUP BY commodity ORDER BY count;
这样做的正确方法是什么?