简体   繁体   中英

Get 5 most frequent values/occuring ids?

I'm trying to get the 5 most occurring IDs in my table, my table looks like this:

+-----------+---------------------+---------+---------+
| mashup_id | mashup_time         | user_id | deal_id |
+-----------+---------------------+---------+---------+
|         1 | 2011-08-24 21:58:22 |       1 |   23870 |
+-----------+---------------------+---------+---------+

I was thinking of doing a query with a sub-query, something that orders by the count of deal_id? Not exactly sure how to go about it though, if anyone can help, thanks!

In (sort of) generic SQL:

SELECT deal_id, COUNT(*)
  FROM your_table
 GROUP BY deal_id
 ORDER BY COUNT(*) DESC
 LIMIT 5

If you meant a different ID field, just substitute it for deal_id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM