Reputation: 440
I'm wondering how I can sort an sql column by frequency, and then pick which row I want. I already know that you need to use:
SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;
To sort from most frequent to least frequent. But how would I select the 1st, 2nd, or 3rd most occurring with using the least amount of different SQL? Could I use PHP?
Is there something akin to:
MAX(VAL)-2
or something close to this, for the 3rd most frequent?
Thanks!
Upvotes: 0
Views: 182
Reputation: 1269883
You would use the limit
clause. For instance:
limit 1 offset 0
would get the first.
limit 1 offset 2
would get the third.
(offset
starts counting at 0 rather than 1.)
Upvotes: 1