获得 table 最常见 table 数据的前 5 个结果

get table results by top 5 of most common table data

示例table数据:

id | name | tool
--------------------
 1 | bob  |   scissor
 2 | mike |   knife
 3 | john |   thread
 4 | joe  |   ruler
 5 | kim  |   marker
 6 | dean |   board
 7 | paul |   knife
 8 | john |   scissor
 9 | kim  |   ruler
 10| mike |   scissor
 11| mike |   board
 12| joe  |   board
 13| paul |   scissor
 13| jake |   marker

并希望从 'tool' 列中获得最常见的前 5 个,这会告诉我类似

的信息
1 - scissor (4)
2 - board (3)
3 - knife (2)
4 - ruler (2)
5 - marker (2)

您可以对工具进行计数,然后按计数限制为 5 个进行排序。

select tool,count(tool) as nr_count
from test
group by tool
order by nr_count desc limit 5;

结果:

tool    nr_count
scissor 4
board   3
knife   2
ruler   2
marker  2

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4c95c05c966d262547f752a2cb599b3b

请注意,您还有另一个工具,上面的查询没有考虑到 2 个结果。您需要在

之前申请另一个订单