我有一个带有room_id和Rental的表名“ room”。

mysql> select * from room;

+---------+--------+
| room_id | rental |
+---------+--------+
|       1 | 2000   |
|       2 | 1890   |
|       3 | 1832   |
|       4 | 1833   |
|       5 | 1850   |
|       6 | 1700   |
|       7 | 2100   |
|       8 | 2000   |
|       9 | 2000   |
|      10 | 2000   |
+----------+--------+
10 rows in set (0.00 sec)


我试图找到最大匹配的行,并从出租列数入。

mysql> select count(*),rental from room group by rental having count(*) >1;

+----------+--------+
| count(*) | rental |
+----------+--------+
|        4 | 2000   |
+----------+--------+
1 row in set (0.08 sec)


但是我的问题是我只想从租金中获得一个最大数字,它具有最大匹配值并像上面那样输出。在上面的查询中将采用count(*)> 1。条件。

最佳答案

按顺序使用和限制1

select count(*) as cnt,rental from room group by rental order by cnt DESC limit 1;

10-06 12:10